Security update for permissions_by_term
[yaffs-website] / vendor / drupal / drupal-driver / src / Drupal / Driver / Fields / Drupal8 / AbstractHandler.php
1 <?php
2
3 namespace Drupal\Driver\Fields\Drupal8;
4
5 use Drupal\Driver\Fields\FieldHandlerInterface;
6
7 /**
8  * Base class for field handlers in Drupal 8.
9  */
10 abstract class AbstractHandler implements FieldHandlerInterface {
11   /**
12    * Field storage definition.
13    *
14    * @var \Drupal\field\Entity\FieldStorageConfig
15    */
16   protected $fieldInfo = NULL;
17
18   /**
19    * Field configuration definition.
20    *
21    * @var \Drupal\field\Entity\FieldConfig
22    */
23   protected $fieldConfig = NULL;
24
25   /**
26    * Constructs an AbstractHandler object.
27    *
28    * @param \stdClass $entity
29    *   The simulated entity object containing field information.
30    * @param string $entity_type
31    *   The entity type.
32    * @param string $field_name
33    *   The field name.
34    *
35    * @throws \Exception
36    *   Thrown when the given field name does not exist on the entity.
37    */
38   public function __construct(\stdClass $entity, $entity_type, $field_name) {
39     $entity_manager = \Drupal::entityManager();
40     $fields = $entity_manager->getFieldStorageDefinitions($entity_type);
41     $this->fieldInfo = $fields[$field_name];
42
43     $bundle_key = $entity_manager->getDefinition($entity_type)->getKey('bundle');
44     $bundle = !empty($entity->$bundle_key) ? $entity->$bundle_key : $entity_type;
45
46     $fields = $entity_manager->getFieldDefinitions($entity_type, $bundle);
47     if (empty($fields[$field_name])) {
48       throw new \Exception(sprintf('The field "%s" does not exist on entity type "%s".', $field_name, $entity_type));
49     }
50     $this->fieldConfig = $fields[$field_name];
51   }
52
53 }