synonyms.module 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * @file
  4. * Provide synonyms feature for content entities.
  5. */
  6. /**
  7. * Implements hook_entity_base_field_info().
  8. */
  9. function synonyms_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  10. if ($entity_type instanceof \Drupal\Core\Entity\ContentEntityTypeInterface) {
  11. $fields = [];
  12. $fields['synonyms'] = \Drupal\Core\Field\BaseFieldDefinition::create('string')
  13. ->setLabel(t('Entity synonyms'))
  14. ->setDescription(t('A list of known entity synonyms.'))
  15. ->setComputed(TRUE)
  16. ->setClass('\Drupal\synonyms\Plugin\SynonymsFieldItemList');
  17. return $fields;
  18. }
  19. }
  20. /**
  21. * Implements hook_views_data().
  22. */
  23. function synonyms_views_data() {
  24. $data = [];
  25. foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) {
  26. if ($entity_type instanceof \Drupal\Core\Entity\ContentEntityTypeInterface && $entity_type->getBaseTable() && $entity_type->getKey('id')) {
  27. $data[$entity_type->getBaseTable()]['synonyms'] = [
  28. 'title' => t('Synonyms of @entity_type', [
  29. '@entity_type' => $entity_type->getLowercaseLabel(),
  30. ]),
  31. 'filter' => [
  32. 'id' => 'synonyms_entity',
  33. 'real field' => $entity_type->getKey('id'),
  34. 'entity_type' => $entity_type->id(),
  35. ],
  36. ];
  37. }
  38. }
  39. return $data;
  40. }