class ParagraphsType

Same name in this branch
  1. 8.x-1.x paragraphs/src/Entity/ParagraphsType.php \Drupal\paragraphs\Entity\ParagraphsType
  2. 8.x-1.x paragraphs/src/Plugin/migrate/source/d7/ParagraphsType.php \Drupal\paragraphs\Plugin\migrate\source\d7\ParagraphsType

Defines the ParagraphsType entity.

Plugin annotation


@ConfigEntityType(
  id = "paragraphs_type",
  label = @Translation("Paragraphs type"),
  label_collection = @Translation("Paragraphs types"),
  label_singular = @Translation("Paragraphs type"),
  label_plural = @Translation("Paragraphs types"),
  label_count = @PluralTranslation(
    singular = "@count Paragraphs type",
    plural = "@count Paragraphs types",
  ),
  handlers = {
    "access" = "Drupal\paragraphs\ParagraphsTypeAccessControlHandler",
    "list_builder" = "Drupal\paragraphs\Controller\ParagraphsTypeListBuilder",
    "form" = {
      "add" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
      "edit" = "Drupal\paragraphs\Form\ParagraphsTypeForm",
      "delete" = "Drupal\paragraphs\Form\ParagraphsTypeDeleteConfirm"
    }
  },
  config_prefix = "paragraphs_type",
  admin_permission = "administer paragraphs types",
  entity_keys = {
    "id" = "id",
    "label" = "label",
  },
  config_export = {
    "id",
    "label",
    "icon_uuid",
    "icon_default",
    "description",
    "behavior_plugins",
  },
  bundle_of = "paragraph",
  links = {
    "edit-form" = "/admin/structure/paragraphs_type/{paragraphs_type}",
    "delete-form" = "/admin/structure/paragraphs_type/{paragraphs_type}/delete",
    "collection" = "/admin/structure/paragraphs_type",
  }
)

Hierarchy

  • class \Drupal\paragraphs\Entity\ParagraphsType extends \Drupal\Core\Config\Entity\ConfigEntityBundleBase implements \Drupal\Core\Entity\EntityWithPluginCollectionInterface, ParagraphsTypeInterface

Expanded class hierarchy of ParagraphsType

27 files declare their use of ParagraphsType
OverviewController.php in paragraphs_collection/src/Controller/OverviewController.php
paragraphs.module in paragraphs/paragraphs.module
Contains paragraphs.module
ParagraphsAddModesTest.php in paragraphs/tests/src/Functional/WidgetStable/ParagraphsAddModesTest.php
ParagraphsAddModesTest.php in paragraphs/tests/src/Functional/WidgetLegacy/ParagraphsAddModesTest.php
ParagraphsBehaviorBase.php in paragraphs/src/ParagraphsBehaviorBase.php

... See full list

File

paragraphs/src/Entity/ParagraphsType.php, line 58

Namespace

Drupal\paragraphs\Entity
View source
class ParagraphsType extends ConfigEntityBundleBase implements ParagraphsTypeInterface, EntityWithPluginCollectionInterface {

  /**
   * The ParagraphsType ID.
   *
   * @var string
   */
  public $id;

  /**
   * The ParagraphsType label.
   *
   * @var string
   */
  public $label;

  /**
   * A brief description of this paragraph type.
   *
   * @var string
   */
  public $description;

  /**
   * UUID of the Paragraphs type icon file.
   *
   * @var string
   */
  protected $icon_uuid;

  /**
   * Default icon encoded as data URL scheme (RFC 2397).
   *
   * @var string
   */
  protected $icon_default;

  /**
   * The Paragraphs type behavior plugins configuration keyed by their id.
   *
   * @var array
   */
  public $behavior_plugins = [];

  /**
   * Holds the collection of behavior plugins that are attached to this
   * Paragraphs type.
   *
   * @var \Drupal\paragraphs\ParagraphsBehaviorCollection
   */
  protected $behaviorCollection;

  /**
   * Restores the icon file from the default icon value.
   *
   * @return \Drupal\file\FileInterface|bool
   *   The icon's file entity or FALSE if no default icon set.
   */
  protected function restoreDefaultIcon() {

    // Default icon data in RFC 2397 format ("data" URL scheme).
    if ($this->icon_default && ($icon_data = fopen($this->icon_default, 'r'))) {

      // Compose the default icon file destination.
      $icon_meta = stream_get_meta_data($icon_data);

      // File extension from MIME, only JPG/JPEG, PNG and SVG expected.
      [
        ,
        $icon_file_ext,
      ] = explode('image/', $icon_meta['mediatype']);

      // SVG special case.
      if ($icon_file_ext == 'svg+xml') {
        $icon_file_ext = 'svg';
      }
      $filesystem = \Drupal::service('file_system');
      $icon_upload_path = ParagraphsTypeInterface::ICON_UPLOAD_LOCATION;
      $icon_file_destination = $icon_upload_path . $this
        ->id() . '-default-icon.' . $icon_file_ext;

      // Check the directory exists before writing data to it.
      $filesystem
        ->prepareDirectory($icon_upload_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);

      // Save the default icon file.
      $icon_file_uri = $filesystem
        ->saveData($icon_data, $icon_file_destination);
      if ($icon_file_uri) {

        // Create the icon file entity.
        $icon_entity_values = [
          'uri' => $icon_file_uri,
          'uid' => \Drupal::currentUser()
            ->id(),
          'uuid' => $this->icon_uuid,
          'status' => FileInterface::STATUS_PERMANENT,
        ];

        // Delete existent icon file if it exists.
        if ($old_icon = $this
          ->getFileByUuid($this->icon_uuid)) {
          $old_icon
            ->delete();
        }
        $new_icon = File::create($icon_entity_values);
        $new_icon
          ->save();
        $this
          ->updateFileIconUsage($new_icon, $old_icon);
        return $new_icon;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getIconFile() {
    if ($this->icon_uuid !== NULL) {
      $icon = $this
        ->getFileByUuid($this->icon_uuid) ?: $this
        ->restoreDefaultIcon();
      if ($icon) {
        return $icon;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getBehaviorPlugins() {
    if (!isset($this->behaviorCollection)) {
      $this->behaviorCollection = new ParagraphsBehaviorCollection(\Drupal::service('plugin.manager.paragraphs.behavior'), $this->behavior_plugins);
    }
    return $this->behaviorCollection;
  }

  /**
   * {@inheritdoc}
   */
  public function getIconUrl() {
    if ($image = $this
      ->getIconFile()) {
      return \Drupal::service('file_url_generator')
        ->generateString($image
        ->getFileUri());
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getBehaviorPlugin($instance_id) {
    return $this
      ->getBehaviorPlugins()
      ->get($instance_id);
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();

    // Add the file icon entity as dependency if a UUID was specified.
    if ($this->icon_uuid && ($file_icon = $this
      ->getIconFile())) {
      $this
        ->addDependency($file_icon
        ->getConfigDependencyKey(), $file_icon
        ->getConfigDependencyName());
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function onDependencyRemoval(array $dependencies) {
    $changed = parent::onDependencyRemoval($dependencies);
    $behavior_plugins = $this
      ->getBehaviorPlugins();
    foreach ($dependencies['module'] as $module) {

      /** @var \Drupal\Component\Plugin\PluginInspectionInterface $plugin */
      foreach ($behavior_plugins as $instance_id => $plugin) {
        $definition = (array) $plugin
          ->getPluginDefinition();

        // If a module providing a behavior plugin is being uninstalled,
        // remove the plugin and dependency so this paragraph bundle is not
        // deleted too.
        if (isset($definition['provider']) && $definition['provider'] === $module) {
          unset($this->behavior_plugins[$instance_id]);
          $this
            ->getBehaviorPlugins()
            ->removeInstanceId($instance_id);
          $changed = TRUE;
        }
      }
    }
    return $changed;
  }

  /**
   * {@inheritdoc}
   */
  public function getEnabledBehaviorPlugins() {
    return $this
      ->getBehaviorPlugins()
      ->getEnabled();
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginCollections() {
    return [
      'behavior_plugins' => $this
        ->getBehaviorPlugins(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->description;
  }

  /**
   * {@inheritdoc}
   */
  public function hasEnabledBehaviorPlugin($plugin_id) {
    $plugins = $this
      ->getBehaviorPlugins();
    if ($plugins
      ->has($plugin_id)) {

      /** @var \Drupal\paragraphs\ParagraphsBehaviorInterface $plugin */
      $plugin = $plugins
        ->get($plugin_id);
      $config = $plugin
        ->getConfiguration();
      return array_key_exists('enabled', $config) && $config['enabled'] === TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    if (!$update || $this->icon_uuid != $this->original->icon_uuid) {

      // Update the file usage for the icon file.
      $new_icon_file = $this->icon_uuid ? $this
        ->getFileByUuid($this->icon_uuid) : FALSE;

      // Update the file usage of the old icon as well if the icon was changed.
      $old_icon_file = $update && $this->original->icon_uuid ? $this
        ->getFileByUuid($this->original->icon_uuid) : FALSE;
      $this
        ->updateFileIconUsage($new_icon_file, $old_icon_file);
    }
    parent::postSave($storage, $update);
  }

  /**
   * Gets the file entity defined by the UUID.
   *
   * @param string $uuid
   *   The file entity's UUID.
   *
   * @return \Drupal\file\FileInterface|null
   *   The file entity. NULL if the UUID is invalid.
   */
  protected function getFileByUuid($uuid) {
    if ($id = \Drupal::service('paragraphs_type.uuid_lookup')
      ->get($uuid)) {
      return $this
        ->entityTypeManager()
        ->getStorage('file')
        ->load($id);
    }
    return NULL;
  }

  /**
   * Updates the icon file usage information.
   *
   * @param \Drupal\file\FileInterface|mixed $new_icon
   *   The new icon file, FALSE on deletion.
   * @param \Drupal\file\FileInterface|mixed $old_icon
   *   (optional) Old icon, on update or deletion.
   */
  protected function updateFileIconUsage($new_icon, $old_icon = FALSE) {

    /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
    $file_usage = \Drupal::service('file.usage');
    if ($new_icon) {

      // Add usage of the new icon file.
      $file_usage
        ->add($new_icon, 'paragraphs', 'paragraphs_type', $this
        ->id());
    }
    if ($old_icon) {

      // Delete usage of the old icon file.
      $file_usage
        ->delete($old_icon, 'paragraphs', 'paragraphs_type', $this
        ->id());
    }
  }

}

Members

Name Modifiers Type Descriptionsort descending Overrides
ParagraphsType::getPluginCollections public function
ParagraphsType::calculateDependencies public function
ParagraphsType::postSave public function
ParagraphsType::onDependencyRemoval public function
ParagraphsType::$description public property A brief description of this paragraph type.
ParagraphsType::$icon_default protected property Default icon encoded as data URL scheme (RFC 2397).
ParagraphsType::getDescription public function Gets the description. Overrides ParagraphsTypeInterface::getDescription
ParagraphsType::getFileByUuid protected function Gets the file entity defined by the UUID.
ParagraphsType::$behaviorCollection protected property Holds the collection of behavior plugins that are attached to this Paragraphs type.
ParagraphsTypeInterface::ICON_UPLOAD_LOCATION constant Icon upload location.
ParagraphsType::restoreDefaultIcon protected function Restores the icon file from the default icon value.
ParagraphsType::getEnabledBehaviorPlugins public function Retrieves all the enabled plugins. Overrides ParagraphsTypeInterface::getEnabledBehaviorPlugins
ParagraphsType::getBehaviorPlugin public function Returns an individual plugin instance. Overrides ParagraphsTypeInterface::getBehaviorPlugin
ParagraphsType::getIconFile public function Returns the icon file entity. Overrides ParagraphsTypeInterface::getIconFile
ParagraphsType::getIconUrl public function Returns the icon's URL. Overrides ParagraphsTypeInterface::getIconUrl
ParagraphsType::getBehaviorPlugins public function Returns the ordered collection of feature plugin instances. Overrides ParagraphsTypeInterface::getBehaviorPlugins
ParagraphsType::hasEnabledBehaviorPlugin public function Returns TRUE if $plugin_id is enabled on this ParagraphType Entity. Overrides ParagraphsTypeInterface::hasEnabledBehaviorPlugin
ParagraphsType::$behavior_plugins public property The Paragraphs type behavior plugins configuration keyed by their id.
ParagraphsType::$id public property The ParagraphsType ID.
ParagraphsType::$label public property The ParagraphsType label.
ParagraphsType::updateFileIconUsage protected function Updates the icon file usage information.
ParagraphsType::$icon_uuid protected property UUID of the Paragraphs type icon file.