You are here

abstract class FieldHandlerBase in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/Plugin/FieldHandlerBase.php \Drupal\cms_content_sync\Plugin\FieldHandlerBase
  2. 2.0.x src/Plugin/FieldHandlerBase.php \Drupal\cms_content_sync\Plugin\FieldHandlerBase

Common base class for field handler plugins.

Hierarchy

Expanded class hierarchy of FieldHandlerBase

See also

\Drupal\cms_content_sync\Annotation\EntityHandler

\Drupal\cms_content_sync\Plugin\FieldHandlerInterface

Plugin API

8 files declare their use of FieldHandlerBase
DefaultFieldHandler.php in src/Plugin/cms_content_sync/field_handler/DefaultFieldHandler.php
DefaultFileHandler.php in src/Plugin/cms_content_sync/field_handler/DefaultFileHandler.php
DefaultFormattedTextHandler.php in src/Plugin/cms_content_sync/field_handler/DefaultFormattedTextHandler.php
DefaultLayoutBuilderHandler.php in src/Plugin/cms_content_sync/field_handler/DefaultLayoutBuilderHandler.php
DefaultLinkHandler.php in src/Plugin/cms_content_sync/field_handler/DefaultLinkHandler.php

... See full list

File

src/Plugin/FieldHandlerBase.php, line 24

Namespace

Drupal\cms_content_sync\Plugin
View source
abstract class FieldHandlerBase extends PluginBase implements ContainerFactoryPluginInterface, FieldHandlerInterface {

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * @var string
   */
  protected $entityTypeName;

  /**
   * @var string
   */
  protected $bundleName;

  /**
   * @var string
   */
  protected $fieldName;

  /**
   * @var \Drupal\Core\Field\FieldDefinitionInterface
   */
  protected $fieldDefinition;

  /**
   * @var array
   *            Additional settings as provided by
   *            {@see FieldHandlerInterface::getHandlerSettings}
   */
  protected $settings;

  /**
   * @var \Drupal\cms_content_sync\Entity\Flow
   */
  protected $flow;

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   *
   * @param array                    $configuration
   *                                                    A configuration array containing information about the plugin instance.
   *                                                    Must contain entity_type_name, bundle_name, field_name, field_definition,
   *                                                    settings and sync (see above).
   * @param string                   $plugin_id
   *                                                    The plugin_id for the plugin instance
   * @param mixed                    $plugin_definition
   *                                                    The plugin implementation definition
   * @param \Psr\Log\LoggerInterface $logger
   *                                                    A logger instance
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->logger = $logger;
    $this->entityTypeName = $configuration['entity_type_name'];
    $this->bundleName = $configuration['bundle_name'];
    $this->fieldName = $configuration['field_name'];
    $this->fieldDefinition = $configuration['field_definition'];
    $this->settings = $configuration['settings'];
    $this->flow = $configuration['sync'];
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('logger.factory')
      ->get('cms_content_sync'));
  }

  /**
   * @return string
   */
  public function getFieldName() {
    return $this->fieldName;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPushOptions() {
    return [
      PushIntent::PUSH_DISABLED,
      PushIntent::PUSH_AUTOMATICALLY,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPullOptions() {
    return [
      PullIntent::PULL_DISABLED,
      PullIntent::PULL_AUTOMATICALLY,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getHandlerSettings($current_values, $type = 'both') {

    // Nothing special here.
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function validateHandlerSettings(array &$form, FormStateInterface $form_state, string $entity_type_name, string $bundle_name, string $field_name, $current_values) {

    // No settings means no validation.
  }

  /**
   * {@inheritdoc}
   */
  public function pull(PullIntent $intent) {
    $action = $intent
      ->getAction();
    $entity = $intent
      ->getEntity();

    // Deletion doesn't require any action on field basis for static data.
    if (SyncIntent::ACTION_DELETE == $action) {
      return false;
    }
    if ($intent
      ->shouldMergeChanges() && !$this
      ->forceMergeOverwrite()) {
      return false;
    }
    if (PullIntent::PULL_AUTOMATICALLY != $this->settings['import']) {
      return false;
    }

    // These fields can't be changed.
    if (!$entity
      ->isNew()) {
      if ('default_langcode' === $this->fieldName) {
        return true;
      }
    }
    $data = $intent
      ->getProperty($this->fieldName);
    if (empty($data)) {
      $entity
        ->set($this->fieldName, null);
    }
    else {
      $entity
        ->set($this->fieldName, $data);
    }
    return true;
  }

  /**
   * {@inheritdoc}
   */
  public function push(PushIntent $intent) {
    $action = $intent
      ->getAction();
    $entity = $intent
      ->getEntity();
    if (PushIntent::PUSH_AUTOMATICALLY != $this->settings['export']) {
      return false;
    }

    // Deletion doesn't require any action on field basis for static data.
    if (SyncIntent::ACTION_DELETE == $action) {
      return false;
    }
    $intent
      ->setProperty($this->fieldName, $entity
      ->get($this->fieldName)
      ->getValue());
    return true;
  }

  /**
   * {@inheritDoc}
   */
  public function definePropertyAtType(IDefineEntityType $type_definition) {
    $type_definition
      ->addObjectProperty($this->fieldName, $this->fieldDefinition
      ->getLabel(), true, $this->fieldDefinition
      ->isRequired());
  }

  /**
   * @return false
   */
  protected function forceMergeOverwrite() {
    return 'changed' == $this->fieldName;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FieldHandlerBase::$bundleName protected property
FieldHandlerBase::$entityTypeName protected property
FieldHandlerBase::$fieldDefinition protected property
FieldHandlerBase::$fieldName protected property
FieldHandlerBase::$flow protected property
FieldHandlerBase::$logger protected property A logger instance.
FieldHandlerBase::$settings protected property Additional settings as provided by {
FieldHandlerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FieldHandlerBase::definePropertyAtType public function Provide the Sync Core with the right property definition so this field can be stored and synchronized. Overrides FieldHandlerInterface::definePropertyAtType 1
FieldHandlerBase::forceMergeOverwrite protected function 1
FieldHandlerBase::getAllowedPullOptions public function Get the allowed pull options. Overrides FieldHandlerInterface::getAllowedPullOptions
FieldHandlerBase::getAllowedPushOptions public function Get the allowed push options. Overrides FieldHandlerInterface::getAllowedPushOptions
FieldHandlerBase::getFieldName public function Overrides FieldHandlerInterface::getFieldName
FieldHandlerBase::getHandlerSettings public function Get the handler settings. Overrides FieldHandlerInterface::getHandlerSettings 3
FieldHandlerBase::pull public function Overrides FieldHandlerInterface::pull 8
FieldHandlerBase::push public function Overrides FieldHandlerInterface::push 8
FieldHandlerBase::validateHandlerSettings public function Validate the settings defined above. $form and $form_state are the same as in the Form API. Overrides FieldHandlerInterface::validateHandlerSettings 1
FieldHandlerBase::__construct public function Constructs a Drupal\rest\Plugin\ResourceBase object. Overrides PluginBase::__construct
FieldHandlerInterface::supports public static function Check if this handler supports the given field instance. 10
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
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 2
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. 4
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.