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