Backup of db before drupal security update
[yaffs-website] / web / core / modules / quickedit / src / MetadataGenerator.php
1 <?php
2
3 namespace Drupal\quickedit;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface;
9 use Drupal\Core\Entity\Entity\EntityViewDisplay;
10
11 /**
12  * Generates in-place editing metadata for an entity field.
13  */
14 class MetadataGenerator implements MetadataGeneratorInterface {
15
16   /**
17    * An object that checks if a user has access to edit a given entity field.
18    *
19    * @var \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface
20    */
21   protected $accessChecker;
22
23   /**
24    * An object that determines which editor to attach to a given field.
25    *
26    * @var \Drupal\quickedit\EditorSelectorInterface
27    */
28   protected $editorSelector;
29
30   /**
31    * The manager for editor plugins.
32    *
33    * @var \Drupal\Component\Plugin\PluginManagerInterface
34    */
35   protected $editorManager;
36
37   /**
38    * Constructs a new MetadataGenerator.
39    *
40    * @param \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface $access_checker
41    *   An object that checks if a user has access to edit a given field.
42    * @param \Drupal\quickedit\EditorSelectorInterface $editor_selector
43    *   An object that determines which editor to attach to a given field.
44    * @param \Drupal\Component\Plugin\PluginManagerInterface $editor_manager
45    *   The manager for editor plugins.
46    */
47   public function __construct(EditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager) {
48     $this->accessChecker = $access_checker;
49     $this->editorSelector = $editor_selector;
50     $this->editorManager = $editor_manager;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function generateEntityMetadata(EntityInterface $entity) {
57     return [
58       'label' => $entity->label(),
59     ];
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function generateFieldMetadata(FieldItemListInterface $items, $view_mode) {
66     $entity = $items->getEntity();
67     $field_name = $items->getFieldDefinition()->getName();
68
69     // Early-return if user does not have access.
70     $access = $this->accessChecker->accessEditEntityField($entity, $field_name);
71     if (!$access) {
72       return ['access' => FALSE];
73     }
74
75     // Early-return if no editor is available.
76     $formatter_id = EntityViewDisplay::collectRenderDisplay($entity, $view_mode)->getRenderer($field_name)->getPluginId();
77     $editor_id = $this->editorSelector->getEditor($formatter_id, $items);
78     if (!isset($editor_id)) {
79       return ['access' => FALSE];
80     }
81
82     // Gather metadata, allow the editor to add additional metadata of its own.
83     $label = $items->getFieldDefinition()->getLabel();
84     $editor = $this->editorManager->createInstance($editor_id);
85     $metadata = [
86       'label' => $label,
87       'access' => TRUE,
88       'editor' => $editor_id,
89     ];
90     $custom_metadata = $editor->getMetadata($items);
91     if (count($custom_metadata)) {
92       $metadata['custom'] = $custom_metadata;
93     }
94
95     return $metadata;
96   }
97
98 }