Paragraphs Editor
EditableGenerator.php
Go to the documentation of this file.
1 <?php
2 
4 
17 
18 /**
19  * Generates EditableField objects for an EditBufferItem.
20  */
22 
23  /**
24  * The paragraphs field value manager for managing elements.
25  *
26  * @var \Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface
27  */
28  protected $fieldValueManager;
29 
30  /**
31  * The dom processor service for creating inline markup.
32  *
33  * @var \Drupal\dom_processor\DomProcessor\DomProcessorInterface
34  */
35  protected $domProcessor;
36 
37  /**
38  * Creates an EditableGenerator object.
39  *
40  * @param \Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface $field_value_manager
41  * The field value manager service for managing elements.
42  * @param \Drupal\dom_processor\DomProcessor\DomProcessorInterface $dom_processor
43  * The Dom processor for generating inline markup.
44  */
45  public function __construct(FieldValueManagerInterface $field_value_manager, DomProcessorInterface $dom_processor) {
46  $this->fieldValueManager = $field_value_manager;
47  $this->domProcessor = $dom_processor;
48  $this->validateElements();
49  }
50 
51  /**
52  * {@inheritdoc}
53  */
54  public function id() {
55  return 'editable';
56  }
57 
58  /**
59  * {@inheritdoc}
60  */
61  public function initialize(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $root_paragraph) {
62  $state->set('editables', []);
63  }
64 
65  /**
66  * {@inheritdoc}
67  */
68  public function processField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field) {
69  $field_config = TypeUtility::ensureFieldConfig($items->getFieldDefinition());
70  $context_id = $data->getContextId($items->getEntity()->uuid(), $field_config->id());
71  if ($context_id) {
72  // Prepare the inline field editable markup.
73  $markup = $this->fieldValueManager->wrapItems($items)->getMarkup();
74  $markup = $this->domProcessor->process($markup, 'paragraphs_editor', 'decorator', ['context_id' => $context_id])->get('markup');
75  $markup = Markup::create($markup);
76 
77  // Create a EditableField object for this field.
78  $context_generator = $state->getGenerator('context');
79  if (!$context_generator instanceof ContextGenerator) {
80  throw new \Exception("Could not locate context generator.");
81  }
82  $context = $context_generator->getContext($context_id);
83  $attributes = [
84  'class' => [$this->fieldValueManager->getElement('field')['flag']],
85  $this->fieldValueManager->getAttributeName('field', '<context>') => $context_id,
86  ];
87  $editable = $this->createEditable($context, $markup, $attributes);
88 
89  // Save the editable in a field mapped state entry.
90  $editables = $state->get('editables');
91  $editables[$items->getEntity()->uuid()][$field_config->id()] = $editable;
92  $state->set('editables', $editables);
93  }
94  }
95 
96  /**
97  * Provides a public getter for editable object lookup.
98  *
99  * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderDataCompilerState $state
100  * The compiler state to lookup the editable in.
101  * @param \Drupal\Core\Field\FieldItemListInterface $items
102  * The field items to lookup the editable metadata for.
103  *
104  * @return \Drupal\paragraphs_editor\WidgetBinder\EditableField
105  * The editable metadata object for the field or NULL if no such object
106  * existed.
107  */
108  public function getEditable(WidgetBinderDataCompilerState $state, FieldItemListInterface $items) {
109  $field_definition = $items->getFieldDefinition();
110  if ($this->fieldValueManager->isParagraphsEditorField($field_definition)) {
111  $uuid = $items->getEntity()->uuid();
112  $field_id = TypeUtility::ensureFieldConfig($field_definition)->id();
113  $editables = $state->get('editables');
114  return !empty($editables[$uuid][$field_id]) ? $editables[$uuid][$field_id] : NULL;
115  }
116  }
117 
118  /**
119  * Factory method for creating editables.
120  *
121  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
122  * The context that will contain edits to the editable field.
123  * @param string $markup
124  * The editor markup to display inside the editable.
125  * @param array $attributes
126  * The attributes to attach to inline editable.
127  *
128  * @return \Drupal\paragraphs_editor\WidgetBinder\EditableField
129  * The created EditableField object.
130  */
131  protected function createEditable(CommandContextInterface $context, $markup, array $attributes) {
132  return new EditableField($context, $markup, $attributes);
133  }
134 
135  /**
136  * Makes sure the field element can handle editables.
137  *
138  * @throw \Exception
139  * Indicates the element configuration passed to the field value manager
140  * service is invalid for use in this generator because the 'flag' key is
141  * not in the 'field' element definition.
142  */
143  protected function validateElements() {
144  if (empty($this->fieldValueManager->getElement('field')['flag'])) {
145  throw new \Exception('Invalid elements array: elements.field.tag required.');
146  }
147  }
148 
149 }
processField(WidgetBinderData $data, WidgetBinderDataCompilerState $state, EntityReferenceFieldItemListInterface $items, $is_editor_field)
static ensureFieldConfig(FieldDefinitionInterface $field_definition=NULL)
Definition: TypeUtility.php:39
createEditable(CommandContextInterface $context, $markup, array $attributes)
__construct(FieldValueManagerInterface $field_value_manager, DomProcessorInterface $dom_processor)
initialize(WidgetBinderData $data, WidgetBinderDataCompilerState $state, ParagraphInterface $root_paragraph)
getEditable(WidgetBinderDataCompilerState $state, FieldItemListInterface $items)