headless_cms-1.0.3/modules/headless_cms_notify/src/Entity/HeadlessNotifyTransport.php

modules/headless_cms_notify/src/Entity/HeadlessNotifyTransport.php
<?php

declare(strict_types=1);

namespace Drupal\headless_cms_notify\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\headless_cms_notify\HeadlessNotifyTransportPluginCollection;
use Drupal\headless_cms_notify\HeadlessNotifyTransportPluginInterface;

/**
 * Defines the HeadlessNotifyTransport entity.
 *
 * @ConfigEntityType(
 *   id = "headless_notify_transport",
 *   label = @Translation("Headless Notify Transport"),
 *   handlers = {
 *     "list_builder" = "Drupal\headless_cms_notify\Controller\HeadlessNotifyTransportListBuilder",
 *     "form" = {
 *       "add" = "Drupal\headless_cms_notify\Form\HeadlessNotifyTransportForm",
 *       "edit" = "Drupal\headless_cms_notify\Form\HeadlessNotifyTransportForm",
 *       "delete" = "Drupal\headless_cms_notify\Form\HeadlessNotifyTransportDeleteForm"
 *     }
 *   },
 *   config_prefix = "transport",
 *   admin_permission = "administer headless_cms settings",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" = "/admin/config/headless-cms/notify/transports/{headless_notify_transport}",
 *     "add-form" = "/admin/config/headless-cms/notify/transports/add",
 *     "edit-form" = "/admin/config/headless-cms/notify/transports/manage/{headless_notify_transport}",
 *     "delete-form" = "/admin/config/headless-cms/notify/transports/manage/{headless_notify_transport}/delete",
 *     "collection" = "/admin/config/headless-cms/notify/transports"
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "transport_plugin",
 *     "transport_plugin_configuration",
 *   }
 * )
 */
class HeadlessNotifyTransport extends ConfigEntityBase implements HeadlessNotifyTransportInterface, EntityWithPluginCollectionInterface {

  use StringTranslationTrait;

  /**
   * The transport ID.
   */
  protected string $id;

  /**
   * The label.
   */
  protected string $label;

  /**
   * The plugin collection that holds the EncryptionMethod plugin.
   */
  protected ?HeadlessNotifyTransportPluginCollection $pluginCollection = NULL;

  /**
   * The configuration of the transport.
   */
  protected array $transport_plugin_configuration = [];

  /**
   * The ID of HeadlessNotifyTransport plugin.
   */
  protected ?string $transport_plugin = NULL;

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

  /**
   * Encapsulates the creation of HeadlessNotifyTransport LazyPluginCollection.
   */
  protected function getPluginCollection() {
    if (!$this->pluginCollection && $this->transport_plugin) {
      $this->pluginCollection = new HeadlessNotifyTransportPluginCollection(
        \Drupal::service('plugin.manager.headless_notify_transport.transports'),
        $this->transport_plugin,
        $this->transport_plugin_configuration,
      );
    }

    return $this->pluginCollection;
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    $errors = $this->validate();
    if (!empty($errors)) {
      throw new \Exception(implode(';', $errors));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getTransportPlugin(): ?HeadlessNotifyTransportPluginInterface {
    if ($this->getTransportPluginId()) {
      return $this->getPluginCollection()->get($this->getTransportPluginId());
    }

    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getTransportPluginId(): ?string {
    return $this->transport_plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function setTransportPlugin(HeadlessNotifyTransportPluginInterface $transport) {
    $this->transport_plugin = $transport->getPluginId();
    $this->getPluginCollection()->addInstanceID($transport->getPluginId());
  }

  /**
   * {@inheritdoc}
   */
  public function validate() {
    $errors = [];

    // Check if the object properties are set correctly.
    if (!$this->getTransportPluginId()) {
      $errors[] = $this->t('No transport selected.');
    }

    // If the properties are set, continue validation.
    if ($this->getTransportPluginId()) {
      // Check if the linked transport plugin is valid.
      $transport = $this->getTransportPlugin();
      if (!$transport) {
        $errors[] = $this->t('The transport plugin linked to this transport does not exist.');
      }
    }

    return $errors;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc