You are here

class DefaultConfigEntityHandler in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/cms_content_sync/entity_handler/DefaultConfigEntityHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultConfigEntityHandler
  2. 2.0.x src/Plugin/cms_content_sync/entity_handler/DefaultConfigEntityHandler.php \Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler\DefaultConfigEntityHandler

Class DefaultConfigEntityHandler, providing a minimalistic implementation for any config entity type.

Plugin annotation


@EntityHandler(
  id = "cms_content_sync_default_config_entity_handler",
  label = @Translation("Default Config"),
  weight = 100
)

Hierarchy

Expanded class hierarchy of DefaultConfigEntityHandler

File

src/Plugin/cms_content_sync/entity_handler/DefaultConfigEntityHandler.php, line 21

Namespace

Drupal\cms_content_sync\Plugin\cms_content_sync\entity_handler
View source
class DefaultConfigEntityHandler extends EntityHandlerBase {

  /**
   * {@inheritdoc}
   */
  public static function supports($entity_type, $bundle) {

    // Whitelist supported entity types.
    $entity_types = [
      'webform',
    ];
    return in_array($entity_type, $entity_types);
  }

  /**
   * @param \EdgeBox\SyncCore\Interfaces\Configuration\IDefineEntityType $definition
   */
  public function updateEntityTypeDefinition(&$definition) {

    // Add properties that are listed as "config_export" keys from the entity
    // type annotation.
    $typeMapping = [
      'uuid' => 'string',
      'boolean' => 'boolean',
      'email' => 'string',
      'integer' => 'integer',
      'float' => 'float',
      'string' => 'string',
      'text' => 'string',
      'label' => 'string',
      'uri' => 'string',
      'mapping' => 'object',
      'sequence' => 'object',
    ];
    foreach ($this
      ->getConfigProperties() as $key => $config) {
      $type = $config['type'];
      if (empty($typeMapping[$type])) {
        continue;
      }
      $remoteType = $typeMapping[$type];
      $multiple = false;
      if ('array' === $remoteType) {
        $type = $config['sequence']['type'];
        if (empty($typeMapping[$type])) {
          continue;
        }
        $remoteType = $typeMapping[$type];
        $multiple = true;
      }
      if ('string' === $remoteType) {
        $definition
          ->addStringProperty($key, $multiple);
      }
      elseif ('boolean' === $remoteType) {
        $definition
          ->addBooleanProperty($key, $multiple);
      }
      elseif ('integer' === $remoteType) {
        $definition
          ->addIntegerProperty($key, $multiple);
      }
      elseif ('float' === $remoteType) {
        $definition
          ->addFloatProperty($key, $multiple);
      }
      else {
        $definition
          ->addObjectProperty($key, $multiple);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAllowedPreviewOptions() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function push(PushIntent $intent, EntityInterface $entity = null) {
    if (!parent::push($intent, $entity)) {
      return false;
    }
    if (!$entity) {
      $entity = $intent
        ->getEntity();
    }

    /**
     * @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
     */
    foreach ($this
      ->getConfigProperties() as $property => $config) {
      $entity_property = $entity
        ->get($property);
      if (is_array($entity_property) && !count($entity_property)) {
        $entity_property = null;
      }
      $intent
        ->setProperty($property, $entity_property);
    }
    return true;
  }

  /**
   * Pull the remote entity.
   *
   * {@inheritdoc}
   */
  public function pull(PullIntent $intent) {
    $action = $intent
      ->getAction();
    if (!parent::pull($intent)) {
      return false;
    }
    if (SyncIntent::ACTION_DELETE === $action) {
      return true;
    }

    /**
     * @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
     */
    $entity = $intent
      ->getEntity();
    $forbidden_fields = $this
      ->getForbiddenFields();
    foreach ($this
      ->getConfigProperties() as $property => $config) {
      if (in_array($property, $forbidden_fields)) {
        continue;
      }
      $entity
        ->set($property, $intent
        ->getProperty($property));
    }
    $entity
      ->save();
    return true;
  }

  /**
   * Get all config properties for this entity type.
   *
   * @return array
   */
  protected function getConfigProperties() {

    /**
     * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
     */
    $entity_type = \Drupal::entityTypeManager()
      ->getDefinition($this->entityTypeName);
    $properties = $entity_type
      ->getPropertiesToExport();
    if (!$properties) {
      return [];
    }
    $config_definition = \Drupal::service('config.typed')
      ->getDefinition($this->entityTypeName . '.' . $this->bundleName . '.*');
    if (empty($config_definition)) {
      return [];
    }
    $mapping = $config_definition['mapping'];
    $result = [];
    foreach ($properties as $property) {

      // Wrong information from webform schema definition...
      // Associative arrays are NOT sequences.
      if ('webform' === $this->entityTypeName && 'access' === $property) {
        $mapping[$property]['type'] = 'mapping';
      }
      $result[$property] = $mapping[$property];
    }
    return $result;
  }

  /**
   * Check whether the entity type supports having a label.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *
   * @return bool
   */
  protected function hasLabelProperty() {
    return true;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultConfigEntityHandler::getAllowedPreviewOptions public function Overrides EntityHandlerInterface::getAllowedPreviewOptions
DefaultConfigEntityHandler::getConfigProperties protected function Get all config properties for this entity type.
DefaultConfigEntityHandler::hasLabelProperty protected function Check whether the entity type supports having a label. Overrides EntityHandlerBase::hasLabelProperty
DefaultConfigEntityHandler::pull public function Pull the remote entity. Overrides EntityHandlerBase::pull
DefaultConfigEntityHandler::push public function Overrides EntityHandlerBase::push
DefaultConfigEntityHandler::supports public static function Check if this handler supports the given entity type. Overrides EntityHandlerInterface::supports
DefaultConfigEntityHandler::updateEntityTypeDefinition public function Overrides EntityHandlerBase::updateEntityTypeDefinition
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
EntityHandlerBase::$bundleName protected property
EntityHandlerBase::$entityTypeName protected property
EntityHandlerBase::$flow protected property A sync instance.
EntityHandlerBase::$logger protected property A logger instance.
EntityHandlerBase::$settings protected property
EntityHandlerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EntityHandlerBase::createNew protected function 1
EntityHandlerBase::deleteEntity protected function Delete a entity.
EntityHandlerBase::getAllowedPullOptions public function Get the allowed pull options. Overrides EntityHandlerInterface::getAllowedPullOptions 2
EntityHandlerBase::getAllowedPushOptions public function Get the allowed push options. Overrides EntityHandlerInterface::getAllowedPushOptions 4
EntityHandlerBase::getForbiddenFields public function Provide a list of fields that are not allowed to be pushed or pulled. These fields typically contain all label fields that are pushed separately anyway (we don't want to set IDs and revision IDs of entities for example, but only use the UUID for… Overrides EntityHandlerInterface::getForbiddenFields 4
EntityHandlerBase::getHandlerSettings public function Get the handler settings. Overrides EntityHandlerInterface::getHandlerSettings 4
EntityHandlerBase::getStaticFields protected function Get a list of fields that can't be updated.
EntityHandlerBase::ignorePull protected function Check if the pull should be ignored. 2
EntityHandlerBase::ignorePush protected function Check if the entity should not be ignored from the push. 2
EntityHandlerBase::isEntityTypeTranslatable protected function
EntityHandlerBase::pushReferencedMenuItems protected function Whether or not menu item references should be pushed.
EntityHandlerBase::REVISION_TRANSLATION_AFFECTED_PROPERTY public constant 2
EntityHandlerBase::saveEntity protected function 1
EntityHandlerBase::setEntityValues protected function Set the values for the pulled entity. 2
EntityHandlerBase::setSourceUrl protected function
EntityHandlerBase::USER_PROPERTY public constant 2
EntityHandlerBase::USER_REVISION_PROPERTY public constant 5
EntityHandlerBase::validateHandlerSettings public function Validate the settings defined above. $form and $form_state are the same as in the Form API. $settings_key is the index at $form['sync_entities'] for this handler instance. Overrides EntityHandlerInterface::validateHandlerSettings
EntityHandlerBase::__construct public function Constructs a Drupal\rest\Plugin\ResourceBase object. Overrides PluginBase::__construct
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.