comparison core/modules/views/src/FieldAPIHandlerTrait.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\views;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6
7 /**
8 * A trait containing helper methods for field definitions.
9 */
10 trait FieldAPIHandlerTrait {
11
12 /**
13 * The field definition.
14 *
15 * @var \Drupal\Core\Field\FieldDefinitionInterface
16 */
17 protected $fieldDefinition;
18
19 /**
20 * The field storage definition.
21 *
22 * @var \Drupal\field\FieldStorageConfigInterface
23 */
24 protected $fieldStorageDefinition;
25
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 /**
48 * Gets the field storage configuration.
49 *
50 * @return \Drupal\field\FieldStorageConfigInterface
51 * The field storage definition used by this handler
52 */
53 protected function getFieldStorageDefinition() {
54 if (!$this->fieldStorageDefinition) {
55 $field_storage_definitions = $this->getEntityManager()->getFieldStorageDefinitions($this->definition['entity_type']);
56 $this->fieldStorageDefinition = $field_storage_definitions[$this->definition['field_name']];
57 }
58 return $this->fieldStorageDefinition;
59 }
60
61 /**
62 * Returns the entity manager.
63 *
64 * @return \Drupal\Core\Entity\EntityManagerInterface
65 * The entity manager service.
66 */
67 protected function getEntityManager() {
68 if (!isset($this->entityManager)) {
69 $this->entityManager = \Drupal::entityManager();
70 }
71 return $this->entityManager;
72 }
73
74 }