Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / linkit / src / Entity / Profile.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Entity\Profile.
6  */
7
8 namespace Drupal\linkit\Entity;
9
10 use Drupal\Core\Config\Entity\ConfigEntityBase;
11 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
12 use Drupal\linkit\AttributeCollection;
13 use Drupal\linkit\MatcherCollection;
14 use Drupal\linkit\MatcherInterface;
15 use Drupal\linkit\ProfileInterface;
16
17 /**
18  * Defines the linkit profile entity.
19  *
20  * @ConfigEntityType(
21  *   id = "linkit_profile",
22  *   label = @Translation("Linkit profile"),
23  *   handlers = {
24  *     "list_builder" = "Drupal\linkit\ProfileListBuilder",
25  *     "form" = {
26  *       "add" = "Drupal\linkit\Form\Profile\AddForm",
27  *       "edit" = "Drupal\linkit\Form\Profile\EditForm",
28  *       "delete" = "Drupal\Core\Entity\EntityDeleteForm"
29  *     }
30  *   },
31  *   admin_permission = "administer linkit profiles",
32  *   config_prefix = "linkit_profile",
33  *   entity_keys = {
34  *     "id" = "id",
35  *     "label" = "label"
36  *   },
37  *   links = {
38  *     "collection" = "/admin/config/content/linkit",
39  *     "edit-form" = "/admin/config/content/linkit/manage/{linkit_profile}",
40  *     "delete-form" = "/admin/config/content/linkit/manage/{linkit_profile}/delete"
41  *   },
42  *   config_export = {
43  *     "id",
44  *     "label",
45  *     "description",
46  *     "attributes",
47  *     "matchers"
48  *   }
49  * )
50  */
51 class Profile extends ConfigEntityBase implements ProfileInterface, EntityWithPluginCollectionInterface {
52
53   /**
54    * The ID of this profile.
55    *
56    * @var string
57    */
58   protected $id;
59
60   /**
61    * The human-readable label of this profile.
62    *
63    * @var string
64    */
65   protected $label;
66
67   /**
68    * Description of this profile.
69    *
70    * @var string
71    */
72   protected $description;
73
74   /**
75    * Configured attribute for this profile.
76    *
77    * An associative array of attribute assigned to the profile, keyed by the
78    * attribute id of each attribute and using the properties:
79    * - id: The plugin ID of the attribute instance.
80    * - status: (optional) A Boolean indicating whether the attribute is enabled
81    *   in the profile. Defaults to FALSE.
82    * - weight: (optional) The weight of the attribute in the profile.
83    *   Defaults to 0.
84    *
85    * @var array
86    */
87   protected $attributes = [];
88
89   /**
90    * Holds the collection of attributes that are attached to this profile.
91    *
92    * @var \Drupal\linkit\AttributeCollection
93    */
94   protected $attributeCollection;
95
96   /**
97    * Configured matchers for this profile.
98    *
99    * An associative array of matchers assigned to the profile, keyed by the
100    * matcher ID of each matcher and using the properties:
101    * - id: The plugin ID of the matchers instance.
102    * - status: (optional) A Boolean indicating whether the matchers is enabled
103    *   in the profile. Defaults to FALSE.
104    * - weight: (optional) The weight of the matchers in the profile.
105    *   Defaults to 0.
106    *
107    * @var array
108    */
109   protected $matchers = [];
110
111   /**
112    * Holds the collection of matchers that are attached to this profile.
113    *
114    * @var \Drupal\linkit\MatcherCollection
115    */
116   protected $matcherCollection;
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getDescription() {
122     return $this->get('description');
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function setDescription($description) {
129     $this->set('description', trim($description));
130     return $this;
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function getAttribute($attribute_id) {
137     return $this->getAttributes()->get($attribute_id);
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   public function getAttributes() {
144     if (!$this->attributeCollection) {
145       $this->attributeCollection = new AttributeCollection($this->getAttributeManager(), $this->attributes);
146       $this->attributeCollection->sort();
147     }
148     return $this->attributeCollection;
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   public function addAttribute(array $configuration) {
155     $this->getAttributes()->addInstanceId($configuration['id'], $configuration);
156     return $configuration['id'];
157   }
158
159   /**
160    * {@inheritdoc}
161    */
162   public function removeAttribute($attribute_id) {
163     unset($this->attributes[$attribute_id]);
164     $this->getAttributes()->removeInstanceId($attribute_id);
165     return $this;
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function setAttributeConfig($attribute_id, array $configuration) {
172     $this->attributes[$attribute_id] = $configuration;
173     $this->getAttributes()->setInstanceConfiguration($attribute_id, $configuration);
174     return $this;
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   public function getMatcher($instance_id) {
181     return $this->getMatchers()->get($instance_id);
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   public function getMatchers() {
188     if (!$this->matcherCollection) {
189       $this->matcherCollection = new MatcherCollection($this->getMatcherManager(), $this->matchers);
190       $this->matcherCollection->sort();
191     }
192     return $this->matcherCollection;
193   }
194
195   /**
196    * {@inheritdoc}
197    */
198   public function addMatcher(array $configuration) {
199     $configuration['uuid'] = $this->uuidGenerator()->generate();
200     $this->getMatchers()->addInstanceId($configuration['uuid'], $configuration);
201     return $configuration['uuid'];
202   }
203
204   /**
205    * {@inheritdoc}
206    */
207   public function removeMatcher(MatcherInterface $matcher) {
208     $this->getMatchers()->removeInstanceId($matcher->getUuid());
209     $this->save();
210     return $this;
211   }
212
213   /**
214    * {@inheritdoc}
215    */
216   public function setMatcherConfig($instance_id, array $configuration) {
217     $this->matchers[$instance_id] = $configuration;
218     $this->getMatchers()->setInstanceConfiguration($instance_id, $configuration);
219     return $this;
220   }
221
222   /**
223    * {@inheritdoc}
224    */
225   public function getPluginCollections() {
226     return array(
227       'attributes' => $this->getAttributes(),
228       'matchers' => $this->getMatchers(),
229     );
230   }
231
232   /**
233    * Returns the attribute manager.
234    *
235    * @return \Drupal\Component\Plugin\PluginManagerInterface
236    *   The attribute manager.
237    */
238   protected function getAttributeManager() {
239     return \Drupal::service('plugin.manager.linkit.attribute');
240   }
241
242   /**
243    * Returns the matcher manager.
244    *
245    * @return \Drupal\Component\Plugin\PluginManagerInterface
246    *   The matcher manager.
247    */
248   protected function getMatcherManager() {
249     return \Drupal::service('plugin.manager.linkit.matcher');
250   }
251
252 }