You are here

class ConfigActionsId in Config Actions 8

Plugin for config id from the active store.

Plugin annotation


@ConfigActionsSource(
  id = "id",
  description = @Translation("Use a configuration id."),
)

Hierarchy

Expanded class hierarchy of ConfigActionsId

File

src/Plugin/ConfigActionsSource/ConfigActionsId.php, line 22

Namespace

Drupal\config_actions\Plugin\ConfigActionsSource
View source
class ConfigActionsId extends ConfigActionsSourceBase {

  /**
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $configStorage;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The cached configuration item for the source.
   * @var \Drupal\Core\Config\Config
   */
  protected $configItem;

  /**
   * Constructs a new ConfigActionsSource object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param ConfigActionsServiceInterface $config_action_service
   *   The ConfigActionsService from the container.
   * @param ConfigFactory $config_factory
   *   The ConfigFactory from the container.
   * @param \Drupal\Core\Config\StorageInterface $config_storage
   *   The configuration storage.
   * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
   *   The configuration manager.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigActionsServiceInterface $config_action_service, ConfigFactory $config_factory, StorageInterface $config_storage, ConfigManagerInterface $config_manager, MessengerInterface $messenger) {
    $this->configFactory = $config_factory;
    $this->configStorage = $config_storage;
    $this->configManager = $config_manager;
    $this->messenger = $messenger;
    parent::__construct($configuration, $plugin_id, $plugin_definition, $config_action_service);
  }

  /**
   * Create a plugin instance from the container
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   * @return static
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    /** @var ConfigActionsServiceInterface $config_action_service */
    $config_action_service = $container
      ->get('config_actions');

    /** @var ConfigFactory $config_factory */
    $config_factory = $container
      ->get('config.factory');

    /** @var StorageInterface $config_storage */
    $config_storage = $container
      ->get('config.storage');

    /** @var ConfigManagerInterface $config_manager */
    $config_manager = $container
      ->get('config.manager');

    /** @var MessengerInterface $messenger */
    $messenger = $container
      ->get('messenger');
    return new static($configuration, $plugin_id, $plugin_definition, $config_action_service, $config_factory, $config_storage, $config_manager, $messenger);
  }

  /**
   * {@inheritdoc}
   */
  public function detect($source) {

    // Check for a valid configuration id.
    return is_string($source) && strpos($source, '.') > 0;
  }

  /**
   * Fetch the configuration item
   *
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\Config
   */
  protected function getConfigItem() {
    if (!isset($this->configItem)) {
      $this->configItem = $this->configFactory
        ->getEditable($this->sourceId);
    }
    return $this->configItem;
  }

  /**
   * {@inheritdoc}
   */
  public function init($source, $base = '') {

    // If we are just starting, or loading a new source, clear the item cache.
    if ($source != $this->sourceId) {
      unset($this->configItem);
    }
    parent::init($source, $base);
  }

  /**
   * {@inheritdoc}
   */
  public function doLoad() {
    $this
      ->setMerge(FALSE);
    $config_item = $this
      ->getConfigItem();
    $data = !empty($config_item) ? $config_item
      ->get() : [];
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function doSave($data) {
    $config_item = $this
      ->getConfigItem();
    if (empty($config_item)) {
      return FALSE;
    }
    else {
      if (empty($data)) {
        $this->messenger
          ->addMessage($this
          ->t('Deleted %name', array(
          '%name' => $this->sourceId,
        )));
        $config_item
          ->delete();
      }
      else {
        $config_item
          ->setData($data);

        // Save any related entity for this config.
        // Taken from ConfigInstaller::createConfiguration()
        if ($entity_type = $this->configManager
          ->getEntityTypeIdByName($this->sourceId)) {

          /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
          $entity_storage = $this->configManager
            ->getEntityTypeManager()
            ->getStorage($entity_type);

          // It is possible that secondary writes can occur during configuration
          // creation. Updates of such configuration are allowed.
          if ($this->configStorage
            ->exists($this->sourceId)) {
            $id = $entity_storage
              ->getIDFromConfigName($this->sourceId, $entity_storage
              ->getEntityType()
              ->getConfigPrefix());
            $entity = $entity_storage
              ->load($id);
            $this->messenger
              ->addMessage($this
              ->t('Updated %name', array(
              '%name' => $this->sourceId,
            )));

            /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
            $entity = $entity_storage
              ->updateFromStorageRecord($entity, $config_item
              ->get());
          }
          else {
            $this->messenger
              ->addMessage($this
              ->t('Created %name', array(
              '%name' => $this->sourceId,
            )));
            $entity = $entity_storage
              ->createFromStorageRecord($config_item
              ->get());
          }
          if ($entity
            ->isInstallable()) {
            $entity
              ->trustData()
              ->save();
          }
        }
        else {
          $this->messenger
            ->addMessage($this
            ->t('Updated %name', array(
            '%name' => $this->sourceId,
          )));
          $config_item
            ->save();
        }
      }
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigActionsId::$configFactory protected property
ConfigActionsId::$configItem protected property The cached configuration item for the source.
ConfigActionsId::$configManager protected property The configuration manager.
ConfigActionsId::$configStorage protected property The configuration manager.
ConfigActionsId::$messenger protected property The Messenger service. Overrides MessengerTrait::$messenger
ConfigActionsId::create public static function Create a plugin instance from the container Overrides ConfigActionsSourceBase::create
ConfigActionsId::detect public function Determine if $source is valid for the specific plugin. Overrides ConfigActionsSourceBase::detect
ConfigActionsId::doLoad public function Load data from the source. Overrides ConfigActionsSourceBase::doLoad
ConfigActionsId::doSave public function Save data to the source. Overrides ConfigActionsSourceBase::doSave
ConfigActionsId::getConfigItem protected function Fetch the configuration item
ConfigActionsId::init public function
ConfigActionsId::__construct public function Constructs a new ConfigActionsSource object. Overrides ConfigActionsSourceBase::__construct
ConfigActionsSourceBase::$actionService protected property
ConfigActionsSourceBase::$changed protected property Determine if sourceData has been changed since last load/save.
ConfigActionsSourceBase::$merge protected property
ConfigActionsSourceBase::$pluginType protected property The type of the plugin instance
ConfigActionsSourceBase::$sourceBase protected property The Base namespace for the source. Plugin specific.
ConfigActionsSourceBase::$sourceData protected property The cached config data for this source instance.
ConfigActionsSourceBase::$sourceId protected property The ID value of the source. Plugin specific.
ConfigActionsSourceBase::getData public function Get the data cached from the last load/save. Overrides ConfigActionsSourceInterface::getData
ConfigActionsSourceBase::getMerge public function Return whether the data from this source will be merged Overrides ConfigActionsSourceInterface::getMerge
ConfigActionsSourceBase::getType public function Return the type of plugin. Overrides ConfigActionsSourceInterface::getType
ConfigActionsSourceBase::isChanged public function Return TRUE if the data has changed since the last load. Overrides ConfigActionsSourceInterface::isChanged
ConfigActionsSourceBase::load public function Load data from the source. Overrides ConfigActionsSourceInterface::load
ConfigActionsSourceBase::save public function Save data to the source. Overrides ConfigActionsSourceInterface::save
ConfigActionsSourceBase::setData public function Set the data cached in this plugin instance. Causes the plugin to be marked as Changed. Overrides ConfigActionsSourceInterface::setData
ConfigActionsSourceBase::setMerge public function Set whether data saved in this source should be merged with existing data Overrides ConfigActionsSourceInterface::setMerge
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.