Skip to content
Snippets Groups Projects
Commit f9d25b3c authored by Dieter Holvoet's avatar Dieter Holvoet
Browse files

Issue #3359708 by DieterHolvoet: Add views integration

parent 822fbd8f
No related branches found
No related tags found
1 merge request!2Issue #3359708: Add views integration
views.filter.taxonomy_enum_reference:
type: views.filter.in_operator
label: 'Taxonomy term enum'
......@@ -58,6 +58,7 @@ class Migration {
$this->entityFieldManager->clearCachedFieldDefinitions();
$fieldStorageConfig->set('type', 'taxonomy_enum');
$fieldStorageConfig->set('module', 'taxonomy_enum');
$fieldStorageConfig->save(TRUE);
FieldStorageConfig::loadByName($entityTypeId, $fieldName)->calculateDependencies()->save();
......
<?php
namespace Drupal\taxonomy_enum\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Filters a view by entity references.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("taxonomy_enum_reference")
*/
class TaxonomyEnumReference extends ManyToOne {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->entityTypeManager = $container->get('entity_type.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public function query() {
$machineNames = $this->value;
$termIds = $this->entityTypeManager
->getStorage('taxonomy_term')
->getQuery()
->condition('vid', $this->options['vid'])
->condition('machine_name', $this->value, 'IN')
->accessCheck(FALSE)
->execute();
$this->value = array_values($termIds);
parent::query();
$this->value = $machineNames;
}
/**
* {@inheritdoc}
*/
public function getValueOptions() {
if ($this->valueOptions !== NULL && $this->valueOptions !== []) {
return $this->valueOptions;
}
if (!$vid = $this->options['vid']) {
$this->valueOptions = [];
return;
}
$vocabulary = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid);
$enumName = $vocabulary->getThirdPartySetting('taxonomy_enum', 'enum_name');
if (!enum_exists($enumName) || !method_exists($enumName, 'from')) {
$this->valueOptions = [];
return;
}
$termStorage = $this->entityTypeManager
->getStorage('taxonomy_term');
$termIds = $termStorage->getQuery()
->condition('vid', $vid)
->accessCheck(FALSE)
->execute();
$terms = $termStorage->loadMultiple($termIds);
$this->valueOptions = array_reduce(
$terms,
function (?array $options, TermInterface $term) {
$options[$term->get('machine_name')->value] = $term->label();
return $options;
},
);
}
/**
* {@inheritdoc}
*/
public function hasExtraOptions() {
return TRUE;
}
/**
* {@inheritdoc}
*/
public function buildExtraOptionsForm(&$form, FormStateInterface $form_state) {
$form['vid'] = [
'#type' => 'select',
'#title' => $this->t('Vocabulary'),
'#options' => array_map(
function ($vocabulary) {
return $vocabulary->label();
},
Vocabulary::loadMultiple(),
),
'#default_value' => $this->options['vid'],
];
}
}
......@@ -7,9 +7,11 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\taxonomy_enum\Exception\InvalidEnumException;
use Drupal\Core\Routing\RouteMatchInterface;
......@@ -265,3 +267,91 @@ function taxonomy_enum_form_taxonomy_term_form_alter(array &$form, FormStateInte
$form['machine_name']['#disabled'] = TRUE;
}
/**
* Implements hook_field_views_data().
*
* @see core_field_views_data()
*/
function taxonomy_enum_field_views_data(FieldStorageConfigInterface $field_storage): array {
$data = views_field_default_views_data($field_storage);
// The code below only deals with the Taxonomy enum field type.
if ($field_storage->getType() != 'taxonomy_enum') {
return $data;
}
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_id = $field_storage->getTargetEntityTypeId();
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = $entity_type_manager->getStorage($entity_type_id)->getTableMapping();
foreach ($data as $table_name => $table_data) {
// Add a relationship to the target entity type.
$target_entity_type_id = $field_storage->getSetting('target_type');
$target_entity_type = $entity_type_manager->getDefinition($target_entity_type_id);
$entity_type_id = $field_storage->getTargetEntityTypeId();
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
$target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();
$field_name = $field_storage->getName();
if ($target_entity_type instanceof ContentEntityTypeInterface) {
foreach ($table_data as $table_field_name => $table_field_data) {
if (isset($table_field_data['filter']) && $table_field_name != 'delta') {
// Create separate views data to allow use of the entity_reference
// filter. Numeric filter should still be available for use.
$entity_reference = $data[$table_name][$table_field_name];
$entity_reference['filter']['id'] = 'taxonomy_enum_reference';
$entity_reference['title'] = $entity_reference['title'] . ' ' . t('as an Enum filter');
$data[$table_name][$table_field_name . '_enum'] = $entity_reference;
}
}
// Provide a relationship for the entity type with the entity reference
// field.
$args = [
'@label' => $target_entity_type->getLabel(),
'@field_name' => $field_name,
];
$data[$table_name][$field_name]['relationship'] = [
'title' => t('@label referenced from @field_name', $args),
'label' => t('@field_name: @label', $args),
'group' => $entity_type->getLabel(),
'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $field_storage->getBundles())]),
'id' => 'standard',
'base' => $target_base_table,
'entity type' => $target_entity_type_id,
'base field' => $target_entity_type->getKey('id'),
'relationship field' => $field_name . '_target_id',
];
// Provide a reverse relationship for the entity type that is referenced by
// the field.
$args['@entity'] = $entity_type->getLabel();
$args['@label'] = $target_entity_type->getSingularLabel();
$pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
$data[$target_base_table][$pseudo_field_name]['relationship'] = [
'title' => t('@entity using @field_name', $args),
'label' => t('@field_name', ['@field_name' => $field_name]),
'group' => $target_entity_type->getLabel(),
'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
'id' => 'entity_reverse',
'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
'entity_type' => $entity_type_id,
'base field' => $entity_type->getKey('id'),
'field_name' => $field_name,
'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
'field field' => $field_name . '_target_id',
'join_extra' => [
[
'field' => 'deleted',
'value' => 0,
'numeric' => TRUE,
],
],
];
}
}
return $data;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment