Paragraphs Editor
WidgetBinderData.php
Go to the documentation of this file.
1 <?php
2 
4 
5 /**
6  * A class for collecting widget binder data models.
7  *
8  * Generators act on the content tree contained in an edit buffer item to
9  * generate widget binder data models which are added to this collection. This
10  * collection can then be delivered over ajax or in a drupalSettings object so
11  * that the front end widget binder library can process the models.
12  */
14 
15  /**
16  * The models to be delivered.
17  *
18  * @var array
19  */
20  protected $data = [];
21 
22  /**
23  * A two-dimensional mapping of contexts.
24  *
25  * The first key is a paragraph uuid, the second key is a field id, and the
26  * value is a context id that corresponds to that editable.
27  *
28  * @var array
29  */
30  protected $contextMap = [];
31 
32  /**
33  * Finds a context id for an editable area based on the uuid and field id.
34  *
35  * @param string $owner_id
36  * The uuid of the object that owns the editable field.
37  * @param string $field_id
38  * The field id of the editable field.
39  *
40  * @return string
41  * The corresponding context id or NULL if no such context existed.
42  */
43  public function getContextId($owner_id, $field_id) {
44  return !empty($this->contextMap[$owner_id][$field_id]) ? $this->contextMap[$owner_id][$field_id] : NULL;
45  }
46 
47  /**
48  * Adds a group of models to a collection.
49  *
50  * @param string $collection_name
51  * The collection to add the models to.
52  * @param array $models
53  * A map where keys are model ids and values are model attributes. Each key
54  * value pair will be added to the collection.
55  */
56  public function addModels($collection_name, array $models) {
57  foreach ($models as $id => $model) {
58  $this->addModel($collection_name, $id, $model);
59  }
60  }
61 
62  /**
63  * Adds a single model to a collection.
64  *
65  * @param string $collection_name
66  * The collection to add the model to.
67  * @param string $id
68  * The id of the model to add.
69  * @param array $model
70  * The attributes of the model to add.
71  */
72  public function addModel($collection_name, $id, array $model) {
73  if ($collection_name == 'context') {
74  if (!empty($model['ownerId']) && !empty($model['fieldId'])) {
75  $map_id = !empty($model['id']) ? $model['id'] : $id;
76  $this->contextMap[$model['ownerId']][$model['fieldId']] = $map_id;
77  }
78  }
79  foreach ($model as $key => $val) {
80  $this->data[$collection_name][$id][$key] = $val;
81  }
82  }
83 
84  /**
85  * Gets all models in a collection.
86  *
87  * @param string $collection_name
88  * The collection to get all models for.
89  *
90  * @return array
91  * A map where keys are model ids and values are model attributes.
92  */
93  public function getModels($collection_name) {
94  return isset($this->data[$collection_name]) ? $this->data[$collection_name] : [];
95  }
96 
97  /**
98  * Gets a specific model.
99  *
100  * @param string $collection_name
101  * The collection the model belongs to.
102  * @param string $id
103  * The id of the model to get.
104  *
105  * @return array
106  * The model attributes, or an empty array if no such model existed.
107  */
108  public function getModel($collection_name, $id) {
109  return isset($this->data[$collection_name][$id]) ? $this->data[$collection_name][$id] : [];
110  }
111 
112  /**
113  * Gets the raw internal data.
114  *
115  * @return array
116  * A two-dimensional map where the first key is the collection name, the
117  * second key is the id and the value is the model's attributes.
118  */
119  public function getData() {
120  return $this->data;
121  }
122 
123  /**
124  * Maerges the models of another data object into this object.
125  *
126  * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData $data
127  * The data to be merged.
128  *
129  * @return \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData
130  * Returns the mreged $this object.
131  */
132  public function merge(WidgetBinderData $data) {
133  foreach ($data->getData() as $collection_name => $models) {
134  $this->addModels($collection_name, $models);
135  }
136  return $this;
137  }
138 
139  /**
140  * Serializes the data models to a widget binder ingestible array.
141  *
142  * @return array
143  * An array of models to be delivered.
144  */
145  public function toArray() {
146  $data = [];
147  foreach ($this->data as $collection_name => $models) {
148  foreach ($models as $id => $model) {
149  $data[] = [
150  'type' => $collection_name,
151  'id' => $id,
152  'attributes' => $model,
153  ];
154  }
155  }
156  return $data;
157  }
158 
159 }