Paragraphs Editor
EditableField.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 /**
9  * Represents a nested (inline) editable within an edit buffer item.
10  */
12 
13  /**
14  * The context the editable's edits are stored in.
15  *
16  * @var \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
17  */
18  protected $context;
19 
20  /**
21  * The markup to render inside the editable area.
22  *
23  * @var string
24  */
25  protected $markup;
26 
27  /**
28  * The attributes to attach to the editable area.
29  *
30  * @var array
31  */
32  protected $attributes;
33 
34  /**
35  * Tracks whether inline editing has already been applied for this editable.
36  *
37  * @var bool
38  */
39  protected $applied = FALSE;
40 
41  /**
42  * Creates an EditableField object.
43  *
44  * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
45  * The context that the editable is associated with. Edits will be stored in
46  * the edit buffer for this context.
47  * @param string $markup
48  * The current field contents for the editable.
49  * @param array $attributes
50  * Attributes to be attached when rendering the editable area. These
51  * attributes will flag that this is an editable field and where the edits
52  * are persisted.
53  */
55  $this->context = $context;
56  $this->markup = $markup;
57  $this->attributes = $attributes;
58  }
59 
60  /**
61  * Applies inline editable attributes and content to a field's render data.
62  *
63  * This is the default for applying inline editing. It can be used within
64  * hook_preprocess_field() to apply inline editing to a field.
65  */
66  public function preprocessField(array &$field_element) {
67  if (empty($field_element['attributes'])) {
68  $field_element['attributes'] = $this->getAttributes();
69  }
70  else {
71  $this->applyAttributes($field_element['attributes']);
72  }
73  $field_element['content'] = $this->getMarkup();
74  $this->applied = TRUE;
75  }
76 
77  /**
78  * Returns whether the editable has been rendered.
79  *
80  * @return bool
81  * True if the editable attributes and markup has already been applied,
82  * FALSE otherwise.
83  */
84  public function isApplied() {
85  return $this->applied;
86  }
87 
88  /**
89  * Getter for the context containing the edits for this editable.
90  *
91  * @return \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
92  * The context containing edits for this editable.
93  */
94  public function getContext() {
95  return $this->context;
96  }
97 
98  /**
99  * Getter for markup to render inside this editable.
100  *
101  * @return string
102  * The editor markup to display inside the editable.
103  */
104  public function getMarkup() {
105  return $this->markup;
106  }
107 
108  /**
109  * Gets a Drupal attributes object containing the editable's attributes.
110  *
111  * @return \Drupal\Core\Template\Attribute
112  * A Drupal Attribute object to be applied to the editable area.
113  */
114  public function getAttributes() {
115  $attributes = new Attribute();
116  return $this->applyAttributes($attributes);
117  }
118 
119  /**
120  * Applies the inline editing attributes to an existing attributes object.
121  *
122  * @return \Drupal\Core\Template\Attribute
123  * The updated Drupal Attribute object.
124  */
125  public function applyAttributes(Attribute $attributes) {
126  foreach ($this->attributes as $name => $value) {
127  if ($name == 'class') {
128  foreach ($value as $class) {
129  $attributes->addClass($class);
130  }
131  }
132  else {
133  $attributes->setAttribute($name, $value);
134  }
135  }
136  return $attributes;
137  }
138 
139  /**
140  * Applies the inline editing attributes to a render element.
141  *
142  * @param array &$element
143  * The element to apply the attributes to.
144  */
145  public function attachAttributes(array &$element) {
146  foreach ($this->attributes as $name => $value) {
147  if ($name == 'class') {
148  foreach ($value as $class) {
149  $element['#attributes']['class'][] = $class;
150  }
151  }
152  else {
153  $element['#attributes'][$name] = $value;
154  }
155  }
156  }
157 
158 }
__construct(CommandContextInterface $context, $markup, array $attributes)