You are here

abstract class ConfigActionsSourceBase in Config Actions 8

Base class for config_actions plugins.

Hierarchy

Expanded class hierarchy of ConfigActionsSourceBase

5 files declare their use of ConfigActionsSourceBase
ConfigActionsArray.php in src/Plugin/ConfigActionsSource/ConfigActionsArray.php
ConfigActionsFile.php in src/Plugin/ConfigActionsSource/ConfigActionsFile.php
ConfigActionsId.php in src/Plugin/ConfigActionsSource/ConfigActionsId.php
ConfigActionsList.php in src/Plugin/ConfigActionsSource/ConfigActionsList.php
ConfigActionsTemplate.php in src/Plugin/ConfigActionsSource/ConfigActionsTemplate.php

File

src/ConfigActionsSourceBase.php, line 13

Namespace

Drupal\config_actions
View source
abstract class ConfigActionsSourceBase extends PluginBase implements ConfigActionsSourceInterface, ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\config_actions\ConfigActionsServiceInterface
   */
  protected $actionService;

  /**
   * The type of the plugin instance
   * @var string
   */
  protected $pluginType;

  /**
   * The cached config data for this source instance.
   * @var array
   */
  protected $sourceData = [];

  /**
   * The ID value of the source.  Plugin specific.
   * @var string
   */
  protected $sourceId = '';

  /**
   * The Base namespace for the source.  Plugin specific.
   * @var string
   */
  protected $sourceBase = '';

  /**
   * Determine if sourceData has been changed since last load/save.
   * @var bool
   */
  protected $changed = FALSE;
  protected $merge = FALSE;

  /** ---------------------------------------------- */

  /** ABSTRACT Functions to be implemented in Plugin */

  /** ---------------------------------------------- */

  /**
   * {@inheritdoc}
   */
  public abstract function doLoad();

  /**
   * {@inheritdoc}
   */
  public abstract function doSave($data);

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

  /** ------------------------------------------- */

  /** GENERAL Functions implemented in Base class */

  /** ------------------------------------------- */

  /**
   * Constructs a new ConfigActionsSource plugin 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.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigActionsServiceInterface $config_action_service) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->actionService = $config_action_service;
    $this->sourceId = !empty($configuration['source']) ? $configuration['source'] : '';
    $this->sourceBase = !empty($configuration['base']) ? $configuration['base'] : '';
    $this->pluginType = $plugin_id;
    $this
      ->setData([], FALSE);
  }

  /**
   * 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');
    return new static($configuration, $plugin_id, $plugin_definition, $config_action_service);
  }

  /**
   * {@inheritdoc}
   */
  public function load() {
    if ($this
      ->isChanged()) {

      // If data has been changed, return it instead of loading fresh
      return $this->sourceData;
    }
    $data = $this
      ->doLoad();
    return $this
      ->setData($data, FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function save($data) {
    if (!empty($this->sourceId)) {
      if ($this
        ->getMerge() && !empty($data)) {
        $existing = $this
          ->doLoad();
        if (!empty($existing)) {
          $data = NestedArray::mergeDeepArray([
            $existing,
            $data,
          ], TRUE);
        }
      }
      $this
        ->setData($data, FALSE);
      return $this
        ->doSave($data);
    }
    else {
      return FALSE;
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function setData($data = [], $changed = TRUE) {
    $this->sourceData = $data;
    $this->changed = $changed;
    return $data;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function setMerge($merge) {
    $this->merge = $merge;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
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::create public static function Create a plugin instance from the container Overrides ContainerFactoryPluginInterface::create 3
ConfigActionsSourceBase::detect abstract public function Determine if $source is valid for the specific plugin. Overrides ConfigActionsSourceInterface::detect 5
ConfigActionsSourceBase::doLoad abstract public function Load data from the source. Overrides ConfigActionsSourceInterface::doLoad 5
ConfigActionsSourceBase::doSave abstract public function Save data to the source. Overrides ConfigActionsSourceInterface::doSave 5
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
ConfigActionsSourceBase::__construct public function Constructs a new ConfigActionsSource plugin object. Overrides PluginBase::__construct 3
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 protected property The messenger. 29
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.