You are here

class ParagraphsViewmodeBehavior in Paragraphs View Modes 8

Class ParagraphsViewmodeBehavior.

Plugin annotation


@ParagraphsBehavior(
  id="paragraphs_viewmode_behavior",
  label=@Translation("Paragraphs View Mode"),
  description=@Translation("A Plugin to allow overriding a paragraph view mode while on default")
)

Hierarchy

Expanded class hierarchy of ParagraphsViewmodeBehavior

File

src/Plugin/paragraphs/Behavior/ParagraphsViewmodeBehavior.php, line 24

Namespace

Drupal\paragraphs_viewmode\Plugin\paragraphs\Behavior
View source
class ParagraphsViewmodeBehavior extends ParagraphsBehaviorBase implements ParagraphsViewmodeBehaviorInterface {

  /**
   * The entity Display Repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a ParagraphsViewmodeBehavior 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 \Drupal\Core\Entity\EntityFieldManager $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
    $this->entityDisplayRepository = $entity_display_repository;
  }

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

  /**
   * {@inheritdoc}
   */
  public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $view_modes = $this->entityDisplayRepository
      ->getViewModeOptions('paragraph');
    $form['override_mode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select which view mode to override'),
      '#options' => $view_modes,
      '#default_value' => $this->configuration['override_mode'],
    ];
    $form['override_available'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Select which view modes are allowable'),
      '#multiple' => TRUE,
      '#options' => $view_modes,
      '#default_value' => $this->configuration['override_available'],
    ];
    $form['override_default'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select default view mode for content to use'),
      '#options' => $view_modes,
      '#default_value' => $this->configuration['override_default'],
    ];
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::validateConfigurationForm($form, $form_state);
    $default = $form_state
      ->getValue('override_default');
    $allowed = array_filter($form_state
      ->getValue('override_available'));
    if (!in_array($default, $allowed)) {
      $form_state
        ->setError($form['override_default'], 'Default view mode must also be selected in allowed view modes');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $override_mode = $form_state
      ->getValue('override_mode');
    $override_available = $form_state
      ->getValue('override_available');
    $override_default = $form_state
      ->getValue('override_default');
    $this->configuration['override_mode'] = $override_mode;
    $this->configuration['override_available'] = array_filter($override_available);
    $this->configuration['override_default'] = $override_default;
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'override_mode' => 'default',
      'override_available' => [
        'default' => 'Default',
      ],
      'override_default' => 'default',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    $all_modes = $this->entityDisplayRepository
      ->getViewModeOptions('paragraph');
    $available_modes = array_filter($this->configuration['override_available']);
    $mode = $paragraph
      ->getBehaviorSetting($this->pluginId, 'view_mode', $this->configuration['override_default']);
    $mode_options = array_intersect_key($all_modes, $available_modes);
    $form['view_mode'] = [
      '#type' => 'select',
      '#title' => 'Select which view mode to use for this paragraph',
      '#options' => $mode_options,
      '#default_value' => $mode,
    ];
    return parent::buildBehaviorForm($paragraph, $form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    $paragraph
      ->setBehaviorSettings($this->pluginId, [
      'view_mode',
      $form_state
        ->getValue('view_mode'),
    ]);
    parent::submitBehaviorForm($paragraph, $form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function entityViewModeAlter(&$view_mode, ParagraphInterface $paragraph, array $context) {
    $override_mode = $this->configuration['override_mode'];
    $new_view_mode = $paragraph
      ->getBehaviorSetting($this->pluginId, 'view_mode', $this->configuration['override_default']);
    if ($view_mode != $override_mode || $override_mode == $new_view_mode) {
      return;
    }
    $view_mode = $new_view_mode;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ParagraphsBehaviorBase::$entityFieldManager protected property The entity field manager.
ParagraphsBehaviorBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ParagraphsBehaviorBase::filterBehaviorFormSubmitValues protected function Removes default behavior form values before storing the user-set ones.
ParagraphsBehaviorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ParagraphsBehaviorBase::getFieldNameOptions public function Returns list of field names for the given paragraph type and field type. Overrides ParagraphsBehaviorInterface::getFieldNameOptions
ParagraphsBehaviorBase::isApplicable public static function Returns if the plugin can be used for the provided Paragraphs type. Overrides ParagraphsBehaviorInterface::isApplicable 1
ParagraphsBehaviorBase::preprocess public function Adds a default set of helper variables for preprocessors and templates. Overrides ParagraphsBehaviorInterface::preprocess
ParagraphsBehaviorBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ParagraphsBehaviorBase::settingsIcon public function Returns a short info icon for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsIcon 1
ParagraphsBehaviorBase::settingsSummary public function Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsSummary 2
ParagraphsBehaviorBase::validateBehaviorForm public function Validates the behavior fields form. Overrides ParagraphsBehaviorInterface::validateBehaviorForm 1
ParagraphsViewmodeBehavior::$entityDisplayRepository protected property The entity Display Repository.
ParagraphsViewmodeBehavior::$entityTypeManager protected property The entity type manager.
ParagraphsViewmodeBehavior::buildBehaviorForm public function Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorBase::buildBehaviorForm
ParagraphsViewmodeBehavior::buildConfigurationForm public function Form constructor. Overrides ParagraphsBehaviorBase::buildConfigurationForm
ParagraphsViewmodeBehavior::create public static function Creates an instance of the plugin. Overrides ParagraphsBehaviorBase::create
ParagraphsViewmodeBehavior::defaultConfiguration public function Gets default configuration for this plugin. Overrides ParagraphsBehaviorBase::defaultConfiguration
ParagraphsViewmodeBehavior::entityViewModeAlter public function Allow plugin to alter the paragraph view mode. Overrides ParagraphsViewmodeBehaviorInterface::entityViewModeAlter
ParagraphsViewmodeBehavior::submitBehaviorForm public function Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorBase::submitBehaviorForm
ParagraphsViewmodeBehavior::submitConfigurationForm public function Form submission handler. Overrides ParagraphsBehaviorBase::submitConfigurationForm
ParagraphsViewmodeBehavior::validateConfigurationForm public function Form validation handler. Overrides ParagraphsBehaviorBase::validateConfigurationForm
ParagraphsViewmodeBehavior::view public function Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface::view
ParagraphsViewmodeBehavior::__construct public function Constructs a ParagraphsViewmodeBehavior object. Overrides ParagraphsBehaviorBase::__construct
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.