Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / Entity / ParagraphsType.php
1 <?php
2
3 namespace Drupal\paragraphs\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
8 use Drupal\Core\Render\Element\File;
9 use Drupal\paragraphs\ParagraphsBehaviorCollection;
10 use Drupal\paragraphs\ParagraphsBehaviorInterface;
11 use Drupal\paragraphs\ParagraphsTypeInterface;
12
13 /**
14  * Defines the ParagraphsType entity.
15  *
16  * @ConfigEntityType(
17  *   id = "paragraphs_type",
18  *   label = @Translation("Paragraphs type"),
19  *   handlers = {
20  *     "list_builder" = "Drupal\paragraphs\Controller\ParagraphsTypeListBuilder",
21  *     "form" = {
22  *       "add" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
23  *       "edit" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
24  *       "delete" = "Drupal\paragraphs\Form\ParagraphsTypeDeleteConfirm"
25  *     }
26  *   },
27  *   config_prefix = "paragraphs_type",
28  *   admin_permission = "administer paragraphs types",
29  *   entity_keys = {
30  *     "id" = "id",
31  *     "label" = "label",
32  *   },
33  *   config_export = {
34  *     "id",
35  *     "label",
36  *     "icon_uuid",
37  *     "description",
38  *     "behavior_plugins",
39  *   },
40  *   bundle_of = "paragraph",
41  *   links = {
42  *     "edit-form" = "/admin/structure/paragraphs_type/{paragraphs_type}",
43  *     "delete-form" = "/admin/structure/paragraphs_type/{paragraphs_type}/delete",
44  *     "collection" = "/admin/structure/paragraphs_type",
45  *   }
46  * )
47  */
48 class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInterface, EntityWithPluginCollectionInterface {
49
50   /**
51    * The ParagraphsType ID.
52    *
53    * @var string
54    */
55   public $id;
56
57   /**
58    * The ParagraphsType label.
59    *
60    * @var string
61    */
62   public $label;
63
64   /**
65    * A brief description of this paragraph type.
66    *
67    * @var string
68    */
69   public $description;
70
71   /**
72    * UUID of the Paragraphs type icon file.
73    *
74    * @var string
75    */
76   protected $icon_uuid;
77
78   /**
79    * The Paragraphs type behavior plugins configuration keyed by their id.
80    *
81    * @var array
82    */
83   public $behavior_plugins = [];
84
85   /**
86    * Holds the collection of behavior plugins that are attached to this
87    * Paragraphs type.
88    *
89    * @var \Drupal\paragraphs\ParagraphsBehaviorCollection
90    */
91   protected $behaviorCollection;
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getIconFile() {
97     if ($this->icon_uuid && $icon = $this->getFileByUuid($this->icon_uuid)) {
98       return $icon;
99     }
100
101     return FALSE;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getBehaviorPlugins() {
108     if (!isset($this->behaviorCollection)) {
109       $this->behaviorCollection = new ParagraphsBehaviorCollection(\Drupal::service('plugin.manager.paragraphs.behavior'), $this->behavior_plugins);
110     }
111     return $this->behaviorCollection;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function getIconUrl() {
118     if ($image = $this->getIconFile()) {
119       return file_create_url($image->getFileUri());
120     }
121
122     return FALSE;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function getBehaviorPlugin($instance_id) {
129     return $this->getBehaviorPlugins()->get($instance_id);
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function calculateDependencies() {
136     parent::calculateDependencies();
137
138     // Add the file icon entity as dependency if a UUID was specified.
139     if ($this->icon_uuid && $file_icon = $this->getIconFile()) {
140       $this->addDependency($file_icon->getConfigDependencyKey(), $file_icon->getConfigDependencyName());
141     }
142
143     return $this->dependencies;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function getEnabledBehaviorPlugins() {
150     return $this->getBehaviorPlugins()->getEnabled();
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function getPluginCollections() {
157     return ['behavior_plugins' => $this->getBehaviorPlugins()];
158   }
159
160   /**
161    * {@inheritdoc}
162    */
163   public function getDescription() {
164     return $this->description;
165   }
166
167   /**
168    * {@inheritdoc}
169    */
170   public function hasEnabledBehaviorPlugin($plugin_id) {
171     $plugins = $this->getBehaviorPlugins();
172     if ($plugins->has($plugin_id)) {
173       /** @var ParagraphsBehaviorInterface $plugin */
174       $plugin = $plugins->get($plugin_id);
175       $config = $plugin->getConfiguration();
176       return (array_key_exists('enabled', $config) && $config['enabled'] === TRUE);
177     }
178     return FALSE;
179   }
180
181   /**
182    * {@inheritdoc}
183    */
184   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
185     // Update the file usage for the icon files.
186     if (!$update || $this->icon_uuid != $this->original->icon_uuid) {
187       // The icon has changed. Update file usage.
188       /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
189       $file_usage = \Drupal::service('file.usage');
190
191       // Add usage of the new icon file, if it exists. It might not exist, if
192       // this Paragraphs type was imported as configuration, or if the icon has
193       // just been removed.
194       if ($this->icon_uuid && $new_icon = $this->getFileByUuid($this->icon_uuid)) {
195         $file_usage->add($new_icon, 'paragraphs', 'paragraphs_type', $this->id());
196       }
197       if ($update) {
198         // Delete usage of the old icon file, if it exists.
199         if ($this->original->icon_uuid && $old_icon = $this->getFileByUuid($this->original->icon_uuid)) {
200           $file_usage->delete($old_icon, 'paragraphs', 'paragraphs_type', $this->id());
201         }
202       }
203     }
204
205     parent::postSave($storage, $update);
206   }
207
208   /**
209    * Gets the file entity defined by the UUID.
210    *
211    * @param string $uuid
212    *   The file entity's UUID.
213    *
214    * @return \Drupal\file\FileInterface|null
215    *  The file entity. NULL if the UUID is invalid.
216    */
217   protected function getFileByUuid($uuid) {
218     $files = $this->entityTypeManager()
219       ->getStorage('file')
220       ->loadByProperties(['uuid' => $uuid]);
221     if ($files) {
222       return current($files);
223     }
224
225     return NULL;
226   }
227
228 }