You are here

class EntitySharePathautoEnhancer in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/jsonapi/FieldEnhancer/EntitySharePathautoEnhancer.php \Drupal\entity_share\Plugin\jsonapi\FieldEnhancer\EntitySharePathautoEnhancer

Prepare path value to be able to handle pathauto metadata.

Plugin annotation


@ResourceFieldEnhancer(
  id = "entity_share_pathauto",
  label = @Translation("Pathauto (Path field only) (Entity Share)"),
  description = @Translation("Prepare path value to be able to handle pathauto metadata."),
  dependencies = {"pathauto"}
)

Hierarchy

Expanded class hierarchy of EntitySharePathautoEnhancer

1 file declares its use of EntitySharePathautoEnhancer
PathautoTest.php in modules/entity_share_client/tests/src/Functional/PathautoTest.php

File

src/Plugin/jsonapi/FieldEnhancer/EntitySharePathautoEnhancer.php, line 24

Namespace

Drupal\entity_share\Plugin\jsonapi\FieldEnhancer
View source
class EntitySharePathautoEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {

  /**
   * Configuration value when exposing current state.
   */
  const EXPOSE_CURRENT_PATHAUTO = 'expose_current_pathauto';

  /**
   * Configuration value when the remote will use its pathauto.
   */
  const FORCE_ENABLE_PATHAUTO = 'force_enable_pathauto';

  /**
   * Configuration value when the remote will not use its pathauto.
   */
  const FORCE_DISABLE_PATHAUTO = 'force_disable_pathauto';

  /**
   * The key value service.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
   */
  protected $keyValue;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, KeyValueFactoryInterface $key_value) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->keyValue = $key_value;
  }

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

  /**
   * {@inheritdoc}
   */
  protected function doUndoTransform($data, Context $context) {
    $configuration = $this
      ->getConfiguration();
    switch ($configuration['behavior']) {
      case self::EXPOSE_CURRENT_PATHAUTO:

        /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
        $field_item = $context['field_item_object'];
        $entity = $field_item
          ->getEntity();
        $entity_type_id = $entity
          ->getEntityTypeId();
        $key = PathautoState::getPathautoStateKey($entity
          ->id());
        $state = $this->keyValue
          ->get("pathauto_state.{$entity_type_id}")
          ->get($key);
        if (!is_null($state)) {
          $data['pathauto'] = $state;
        }
        else {
          $data['pathauto'] = 0;
        }
        break;
      case self::FORCE_ENABLE_PATHAUTO:
        $data['pathauto'] = 1;
        break;
      case self::FORCE_DISABLE_PATHAUTO:
        $data['pathauto'] = 0;
        break;
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  protected function doTransform($value, Context $context) {
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getOutputJsonSchema() {
    return [
      'type' => 'object',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'behavior' => self::EXPOSE_CURRENT_PATHAUTO,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $resource_field_info) {
    $settings = empty($resource_field_info['enhancer']['settings']) ? $this
      ->getConfiguration() : $resource_field_info['enhancer']['settings'];
    return [
      'behavior' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Expose pathauto value'),
        '#description' => $this
          ->t('If pathauto state is 0, the client website will use the path on the server website (generated with Pathauto or not). If pathauto state is 1, the client website will generate a new alias according to its own configuration if Pathauto patterns are used on the client website.'),
        '#required' => TRUE,
        '#options' => [
          self::EXPOSE_CURRENT_PATHAUTO => $this
            ->t('Expose current pathauto state'),
          self::FORCE_ENABLE_PATHAUTO => $this
            ->t('Force to expose a pathauto state enabled'),
          self::FORCE_DISABLE_PATHAUTO => $this
            ->t('Force to expose a pathauto state disabled'),
        ],
        '#default_value' => $settings['behavior'],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntitySharePathautoEnhancer::$keyValue protected property The key value service.
EntitySharePathautoEnhancer::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EntitySharePathautoEnhancer::defaultConfiguration public function
EntitySharePathautoEnhancer::doTransform protected function
EntitySharePathautoEnhancer::doUndoTransform protected function
EntitySharePathautoEnhancer::EXPOSE_CURRENT_PATHAUTO constant Configuration value when exposing current state.
EntitySharePathautoEnhancer::FORCE_DISABLE_PATHAUTO constant Configuration value when the remote will not use its pathauto.
EntitySharePathautoEnhancer::FORCE_ENABLE_PATHAUTO constant Configuration value when the remote will use its pathauto.
EntitySharePathautoEnhancer::getOutputJsonSchema public function
EntitySharePathautoEnhancer::getSettingsForm public function
EntitySharePathautoEnhancer::__construct public function