Commit 3268c0d0 authored by generalredneck's avatar generalredneck

WIP: Adding work done sinc last time...

parent 61bd15f0
<?php
namespace Drupal\views_natural_sort\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a Index record source plugin item annotation object.
*
* @see \Drupal\views_natural_sort\Plugin\IndexRecordSourcePluginManager
* @see plugin_api
*
* @Annotation
*/
class IndexRecordSourcePlugin extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
/**
* The label of the plugin.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $label;
}
......@@ -2,14 +2,18 @@
namespace Drupal\views_natural_sort;
use Drupal\views_natural_sort\Plugin\IndexRecordSourcePluginInterface as EntrySourcePluginInterface;
class IndexRecordType {
protected $entityType;
protected $field;
protected $entrySourcePlugin;
public function __construct($entity_type_id, $field_machine_name) {
$this->setEntityType($entity_type_id);
$this->setField($field_machine_name);
public function __construct($entity_type_id, $field_machine_name, $entry_source_plugin) {
$this->setEntityType($entity_type_id)
->setField($field_machine_name)
->setEntrySourcePlugin($entry_source_plugin);
}
public function getEntityType() {
......@@ -18,6 +22,7 @@ class IndexRecordType {
public function setEntityType($entity_type_id) {
$this->entityType = $entity_type_id;
return $this;
}
public function getField() {
......@@ -26,5 +31,17 @@ class IndexRecordType {
public function setField($field_machine_name) {
$this->field = $field_machine_name;
return $this;
}
public function getEntrySourcePlugin() {
return $this->entrySourcePlugin;
}
public function setEntrySourcePlugin(EntrySourcePluginInterface $entry_source_plugin) {
$this->entrySourcePlugin = $entry_source_plugin;
return $this;
}
}
<?php
namespace Drupal\views_natural_sort\Plugin\IndexRecordSourcePlugin;
use Drupal\views_natural_sort\Plugin\IndexRecordSourcePluginBase as EntrySourcePlugin;
use Drupal\views\ViewsData;
use Drupal\views_natural_sort\IndexRecordType as EntryType;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @IndexRecordSourcePlugin (
* id = "entity_property",
* label = @Translation("Entity Property")
* )
*/
class EntityProperty extends EntrySourcePlugin {
protected $viewsData;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('views.views_data')
);
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewsData $views_data) {
$this->viewsData = $views_data;
}
public function getEntryTypes() {
static $types = [];
if (empty($types)) {
foreach ($this->getViewsSupportedEntityProperties() as $entity_type => $properties) {
foreach ($properties as $property => $schema_info) {
$entry_types[] = new EntryType($entity_type, $property, $this);
}
}
}
return $types;
}
public function getSupportedEntityProperties() {
static $supported_properties = [];
if (empty($supported_properties)) {
foreach ($this->entityFieldManager->getFieldMap() as $entity_type => $info) {
foreach ($info as $field_name => $field_info) {
if ($field_info['type'] == 'string' || $field_info['type'] == 'string_long') {
$fieldConfigs = $this->entityFieldManager->getFieldDefinitions($entity_type, reset($field_info['bundles']));
$fieldConfig = $fieldConfigs[$field_name];
if (empty($supported_properties[$entity_type])) {
$supported_properties[$entity_type] = [];
}
$base_table = $this->getViewsBaseTable($fieldConfig);
if (empty($base_table)) {
continue;
}
$supported_properties[$entity_type][$field_name] = [
'base_table' => $base_table,
// This may not be techincally correct. Research Further.
'schema_field' => $field_name,
];
}
}
}
}
return $supported_properties;
}
public function getViewsSupportedEntityProperties() {
static $views_supported_properties = [];
if (empty($views_supported_properties)) {
$supported_entity_properties = $this->getSupportedEntityProperties();
$views_data = $this->viewsData->getAll();
if (empty($views_data)) {
return FALSE;
}
foreach ($supported_entity_properties as $entity => $properties) {
foreach ($properties as $property => $schema_info) {
if (!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]) &&
!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']) &&
!empty($views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']['id']) &&
$views_data[$schema_info['base_table']][$schema_info['schema_field']]['sort']['id'] == 'natural') {
$views_supported_properties[$entity][$property] = $schema_info;
}
}
}
}
return $views_supported_properties;
}
/**
* @see EntityViewsData::getViewsData()
*/
public function getViewsBaseTable($fieldDefinition) {
$entityType = $this->entityTypeManager->getDefinition($fieldDefinition->getTargetEntityTypeId());
$base_table = $entityType->getBaseTable() ?: $entityType->id();
$views_revision_base_table = NULL;
$revisionable = $entityType->isRevisionable();
$base_field = $entityType->getKey('id');
$revision_table = '';
if ($revisionable) {
$revision_table = $entityType->getRevisionTable() ?: $entityType->id() . '_revision';
}
$translatable = $entityType->isTranslatable();
$data_table = '';
if ($translatable) {
$data_table = $entityType->getDataTable() ?: $entityType->id() . '_field_data';
}
// Some entity types do not have a revision data table defined, but still
// have a revision table name set in
// \Drupal\Core\Entity\Sql\SqlContentEntityStorage::initTableLayout() so we
// apply the same kind of logic.
$revision_data_table = '';
if ($revisionable && $translatable) {
$revision_data_table = $entityType->getRevisionDataTable() ?: $entityType->id() . '_field_revision';
}
$revision_field = $entityType->getKey('revision');
$views_base_table = $base_table;
if ($data_table) {
$views_base_table = $data_table;
}
//TODO Add support for finding Fields API Fields base tables. See views.views.inc.
return $views_base_table;
}
}
<?php
namespace Drupal\views_natural_sort\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
/**
* Base class for Index record source plugin plugins.
*/
abstract class IndexRecordSourcePluginBase extends PluginBase implements IndexRecordSourcePluginInterface, ContainerFactoryPluginInterface {
// Add common methods and abstract methods for your plugin type here.
}
<?php
namespace Drupal\views_natural_sort\Plugin;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines an interface for Index record source plugin plugins.
*/
interface IndexRecordSourcePluginInterface extends PluginInspectionInterface {
/**
* Gets all the types that use this source plugin.
*
* @return array
* Array of IndexRecordType objects.
*/
public function getEntryTypes();
}
<?php
namespace Drupal\views_natural_sort\Plugin;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Provides the Index record source plugin plugin manager.
*/
class IndexRecordSourcePluginManager extends DefaultPluginManager {
/**
* Constructs a new IndexRecordSourcePluginManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/IndexRecordSourcePlugin', $namespaces, $module_handler, 'Drupal\views_natural_sort\Plugin\IndexRecordSourcePluginInterface', 'Drupal\views_natural_sort\Annotation\IndexRecordSourcePlugin');
$this->alterInfo('views_natural_sort_index_record_source_plugin_info');
$this->setCacheBackend($cache_backend, 'views_natural_sort_index_record_source_plugin_plugins');
}
}
......@@ -14,6 +14,7 @@ use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\views\ViewsData;
use Drupal\views_natural_sort\Plugin\IndexRecordContentTransformationManager as TransformationManager;
use Drupal\views_natural_sort\Plugin\IndexRecordSourcePluginManager as EntrySourcePluginManager;
/**
* Service that manages Views Natural Sort records.
......@@ -23,7 +24,7 @@ class ViewsNaturalSortService {
/**
* Constructor.
*/
public function __construct(TransformationManager $transformationManager, ConfigFactory $configFactory, ModuleHandlerInterface $moduleHandler, LoggerChannelFactory $loggerFactory, Connection $database, ViewsData $viewsData, QueueFactory $queue, QueueWorkerManagerInterface $queueManager, EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, FieldTypePluginManager $fieldTypeManager) {
public function __construct(TransformationManager $transformationManager, ConfigFactory $configFactory, ModuleHandlerInterface $moduleHandler, LoggerChannelFactory $loggerFactory, Connection $database, ViewsData $viewsData, QueueFactory $queue, QueueWorkerManagerInterface $queueManager, EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, FieldTypePluginManager $fieldTypeManager, EntrySourcePluginManager $entry_source_plugin_manager) {
$this->configFactory = $configFactory;
$this->moduleHandler = $moduleHandler;
$this->loggerFactory = $loggerFactory->get('views_natural_sort');
......@@ -35,6 +36,7 @@ class ViewsNaturalSortService {
$this->entityFieldManager = $entityFieldManager;
$this->entityTypeManager = $entityTypeManager;
$this->fieldTypeManager = $fieldTypeManager;
$this->entrySourcePluginManager = $entry_source_plugin_manager;
}
/**
......@@ -70,6 +72,21 @@ class ViewsNaturalSortService {
return $transformations;
}
public function getEntryTypes() {
static $entry_types = [];
if (empty($entry_types)) {
$entry_sources = $this->entrySourcePluginManager->getDefinitions();
foreach ($entry_sources as $plugin_id => $definition) {
$entry_source_plugin = $this->entrySourcePluginManager->createInstance($plugin_id);
foreach ($entry_source_plugin->getEntryTypes() as $entry_type) {
$entry_types[$entry_type->getEntityType() . '|' . $entry_type->getField()] = $entry_type;
}
}
$this->moduleHandler->alter('views_natural_sort_get_entry_types', $entry_types);
}
return $entry_types;
}
/**
* Retrieve the full list of entities and properties that can be supported.
*
......@@ -116,33 +133,6 @@ class ViewsNaturalSortService {
* ]
* )
*/
public function getSupportedEntityProperties() {
static $supported_properties = [];
if (empty($supported_properties)) {
foreach ($this->entityFieldManager->getFieldMap() as $entity_type => $info) {
foreach ($info as $field_name => $field_info) {
if ($field_info['type'] == 'string' || $field_info['type'] == 'string_long') {
$fieldConfigs = $this->entityFieldManager->getFieldDefinitions($entity_type, reset($field_info['bundles']));
$fieldConfig = $fieldConfigs[$field_name];
if (empty($supported_properties[$entity_type])) {
$supported_properties[$entity_type] = [];
}
$base_table = $this->getViewsBaseTable($fieldConfig);
if (empty($base_table)) {
continue;
}
$supported_properties[$entity_type][$field_name] = [
'base_table' => $base_table,
// This may not be techincally correct. Research Further.
'schema_field' => $field_name,
];
}
}
}
}
return $supported_properties;
}
public function getViewsSupportedEntityProperties() {
static $views_supported_properties = [];
......@@ -235,66 +225,6 @@ class ViewsNaturalSortService {
return $record;
}
/**
* @see EntityViewsData::getViewsData()
* @see views_fielf_default_views_data()
*
* @todo make this work for revisions as well. Probably secondary function and added to supported properties format and taken care of somehow in hook_views_data_alter.
*/
public function getViewsBaseTable($fieldDefinition) {
$entityType = $this->entityTypeManager->getDefinition($fieldDefinition->getTargetEntityTypeId());
if ($fieldDefinition instanceof \Drupal\field\Entity\FieldConfig) {
$field_storage = $fieldDefinition->getFieldStorageDefinition();
// Check the field type is available.
if (!$this->fieldTypeManager->hasDefinition($field_storage->getType())) {
return FALSE;
}
// Check the field storage has fields.
if (!$field_storage->getBundles()) {
return FALSE;
}
// Ignore custom storage too.
if ($field_storage->hasCustomStorage()) {
return FALSE;
}
// Check whether the entity type storage is supported.
// Change this to a required service.
include_once \Drupal::service('module_handler')->getModule('views')->getPath(). '/views.views.inc';
$storage = _views_field_get_entity_type_storage($field_storage);
if (!$storage) {
return FALSE;
}
if (!$base_table = $entityType->getBaseTable()) {
return FALSE;
}
$field_name = $field_storage->getName();
$entity_type_id = $field_storage->getTargetEntityTypeId();
$views_base_table = "{$entity_type_id}__{$field_name}";
}
else {
$entityType = $this->entityTypeManager->getDefinition($fieldDefinition->getTargetEntityTypeId());
$base_table = $entityType->getBaseTable() ?: $entityType->id();
$views_revision_base_table = NULL;
$revisionable = $entityType->isRevisionable();
$base_field = $entityType->getKey('id');
$translatable = $entityType->isTranslatable();
$data_table = '';
if ($translatable) {
$data_table = $entityType->getDataTable() ?: $entityType->id() . '_field_data';
}
$views_base_table = $base_table;
if ($data_table) {
$views_base_table = $data_table;
}
//TODO Add support for finding Fields API Fields base tables. See views.views.inc.
}
return $views_base_table;
}
}
......
......@@ -43,13 +43,16 @@ function views_natural_sort_module_implements_alter(&$implements, $hook) {
*/
function views_natural_sort_views_natural_sort_get_entry_types() {
$service = Drupal::service('views_natural_sort.service');
/*
$supported_entity_properties = $service->getViewsSupportedEntityProperties();
$entry_types = [];
foreach ($supported_entity_properties as $entity_type => $properties) {
foreach ($properties as $property => $schema_info) {
$entry_types = new IndexRecordType($entity_type, $property);
}
}
}*/
$service->getEntryTypes();
return $entry_types;
}
......
......@@ -2,6 +2,9 @@ services:
plugin.manager.vns_transformation:
class: Drupal\views_natural_sort\Plugin\IndexRecordContentTransformationManager
parent: default_plugin_manager
plugin.manager.vns_entry_source_plugin:
class: Drupal\views_natural_sort\Plugin\IndexRecordSourcePluginManager
parent: default_plugin_manager
views_natural_sort.service:
class: Drupal\views_natural_sort\ViewsNaturalSortService
arguments:
......@@ -16,3 +19,4 @@ services:
- '@entity_field.manager'
- '@entity_type.manager'
- '@plugin.manager.field.field_type'
- '@plugin.manager.vns_entry_source_plugin'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment