3 namespace Drupal\paragraphs\Entity;
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;
14 * Defines the ParagraphsType entity.
17 * id = "paragraphs_type",
18 * label = @Translation("Paragraphs type"),
20 * "list_builder" = "Drupal\paragraphs\Controller\ParagraphsTypeListBuilder",
22 * "add" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
23 * "edit" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
24 * "delete" = "Drupal\paragraphs\Form\ParagraphsTypeDeleteConfirm"
27 * config_prefix = "paragraphs_type",
28 * admin_permission = "administer paragraphs types",
40 * bundle_of = "paragraph",
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",
48 class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInterface, EntityWithPluginCollectionInterface {
51 * The ParagraphsType ID.
58 * The ParagraphsType label.
65 * A brief description of this paragraph type.
72 * UUID of the Paragraphs type icon file.
79 * The Paragraphs type behavior plugins configuration keyed by their id.
83 public $behavior_plugins = [];
86 * Holds the collection of behavior plugins that are attached to this
89 * @var \Drupal\paragraphs\ParagraphsBehaviorCollection
91 protected $behaviorCollection;
96 public function getIconFile() {
97 if ($this->icon_uuid && $icon = $this->getFileByUuid($this->icon_uuid)) {
107 public function getBehaviorPlugins() {
108 if (!isset($this->behaviorCollection)) {
109 $this->behaviorCollection = new ParagraphsBehaviorCollection(\Drupal::service('plugin.manager.paragraphs.behavior'), $this->behavior_plugins);
111 return $this->behaviorCollection;
117 public function getIconUrl() {
118 if ($image = $this->getIconFile()) {
119 return file_create_url($image->getFileUri());
128 public function getBehaviorPlugin($instance_id) {
129 return $this->getBehaviorPlugins()->get($instance_id);
135 public function calculateDependencies() {
136 parent::calculateDependencies();
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());
143 return $this->dependencies;
149 public function getEnabledBehaviorPlugins() {
150 return $this->getBehaviorPlugins()->getEnabled();
156 public function getPluginCollections() {
157 return ['behavior_plugins' => $this->getBehaviorPlugins()];
163 public function getDescription() {
164 return $this->description;
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);
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');
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());
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());
205 parent::postSave($storage, $update);
209 * Gets the file entity defined by the UUID.
211 * @param string $uuid
212 * The file entity's UUID.
214 * @return \Drupal\file\FileInterface|null
215 * The file entity. NULL if the UUID is invalid.
217 protected function getFileByUuid($uuid) {
218 $files = $this->entityTypeManager()
220 ->loadByProperties(['uuid' => $uuid]);
222 return current($files);