3 namespace Drupal\quickedit;
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;
12 * Generates in-place editing metadata for an entity field.
14 class MetadataGenerator implements MetadataGeneratorInterface {
17 * An object that checks if a user has access to edit a given entity field.
19 * @var \Drupal\quickedit\Access\EditEntityFieldAccessCheckInterface
21 protected $accessChecker;
24 * An object that determines which editor to attach to a given field.
26 * @var \Drupal\quickedit\EditorSelectorInterface
28 protected $editorSelector;
31 * The manager for editor plugins.
33 * @var \Drupal\Component\Plugin\PluginManagerInterface
35 protected $editorManager;
38 * Constructs a new MetadataGenerator.
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.
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;
56 public function generateEntityMetadata(EntityInterface $entity) {
58 'label' => $entity->label(),
65 public function generateFieldMetadata(FieldItemListInterface $items, $view_mode) {
66 $entity = $items->getEntity();
67 $field_name = $items->getFieldDefinition()->getName();
69 // Early-return if user does not have access.
70 $access = $this->accessChecker->accessEditEntityField($entity, $field_name);
72 return ['access' => FALSE];
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];
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);
88 'editor' => $editor_id,
90 $custom_metadata = $editor->getMetadata($items);
91 if (count($custom_metadata)) {
92 $metadata['custom'] = $custom_metadata;