FieldAPIHandlerTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\views;
  3. use Drupal\Core\Field\BaseFieldDefinition;
  4. /**
  5. * A trait containing helper methods for field definitions.
  6. */
  7. trait FieldAPIHandlerTrait {
  8. /**
  9. * The field definition.
  10. *
  11. * @var \Drupal\Core\Field\FieldDefinitionInterface
  12. */
  13. protected $fieldDefinition;
  14. /**
  15. * The field storage definition.
  16. *
  17. * @var \Drupal\field\FieldStorageConfigInterface
  18. */
  19. protected $fieldStorageDefinition;
  20. /**
  21. * The entity field manager.
  22. *
  23. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  24. */
  25. protected $entityFieldManager;
  26. /**
  27. * Gets the field definition.
  28. *
  29. * A View works on an entity type across bundles, and thus only has access to
  30. * field storage definitions. In order to be able to use widgets and
  31. * formatters, we create a generic field definition out of that storage
  32. * definition.
  33. *
  34. * @see BaseFieldDefinition::createFromFieldStorageDefinition()
  35. *
  36. * @return \Drupal\Core\Field\FieldDefinitionInterface
  37. * The field definition used by this handler.
  38. */
  39. protected function getFieldDefinition() {
  40. if (!$this->fieldDefinition) {
  41. $field_storage_config = $this->getFieldStorageDefinition();
  42. $this->fieldDefinition = BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_config);
  43. }
  44. return $this->fieldDefinition;
  45. }
  46. /**
  47. * Gets the field storage configuration.
  48. *
  49. * @return \Drupal\field\FieldStorageConfigInterface
  50. * The field storage definition used by this handler
  51. */
  52. protected function getFieldStorageDefinition() {
  53. if (!$this->fieldStorageDefinition) {
  54. $field_storage_definitions = $this->getEntityFieldManager()->getFieldStorageDefinitions($this->definition['entity_type']);
  55. $this->fieldStorageDefinition = $field_storage_definitions[$this->definition['field_name']];
  56. }
  57. return $this->fieldStorageDefinition;
  58. }
  59. /**
  60. * Returns the entity manager.
  61. *
  62. * @return \Drupal\Core\Entity\EntityManagerInterface
  63. * The entity manager service.
  64. *
  65. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  66. * \Drupal\views\FieldAPIHandlerTrait::getEntityFieldManager() instead.
  67. *
  68. * @see https://www.drupal.org/node/2549139
  69. */
  70. protected function getEntityManager() {
  71. @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed in drupal:9.0.0. Use \Drupal\views\FieldAPIHandlerTrait::getEntityFieldManager() instead. See https://www.drupal.org/node/2549139', E_USER_DEPRECATED);
  72. if (!isset($this->entityManager)) {
  73. $this->entityManager = \Drupal::entityManager();
  74. }
  75. return $this->entityManager;
  76. }
  77. /**
  78. * Returns the entity field manager.
  79. *
  80. * @return \Drupal\Core\Entity\EntityManagerInterface
  81. * The entity field manager.
  82. */
  83. protected function getEntityFieldManager() {
  84. if (!isset($this->entityFieldManager)) {
  85. $this->entityFieldManager = \Drupal::service('entity_field.manager');
  86. }
  87. return $this->entityFieldManager;
  88. }
  89. }