class ParagraphsAnchorPlugin

Provides a Paragraphs Anchor plugin.

Plugin annotation


@ParagraphsBehavior(
  id = "anchor",
  label = @Translation("Anchor"),
  description = @Translation("Allows to set ID attribute that can be used as jump position in URLs."),
  weight = 3
)

Hierarchy

Expanded class hierarchy of ParagraphsAnchorPlugin

File

paragraphs_collection/modules/paragraphs_collection_demo/src/Plugin/paragraphs/Behavior/ParagraphsAnchorPlugin.php, line 24

Namespace

Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior
View source
class ParagraphsAnchorPlugin extends ParagraphsBehaviorBase {

  /**
   * Config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * ParagraphsSliderPlugin constructor.
   *
   * @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\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
    $this->configFactory = $config_factory;
  }

  /**
   * {@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('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
    if ($anchor = $paragraph
      ->getBehaviorSetting($this
      ->getPluginId(), 'anchor')) {
      $build['#attributes']['id'] = 'scrollto-' . $anchor;
      $build['#attributes']['class'][] = 'paragraphs-anchor-link';

      // @todo Make UI for global configuration.
      // @see https://www.drupal.org/node/2856912.
      if ($this->configFactory
        ->get('paragraphs_collection_demo.settings')
        ->get('anchor')['show_permalink']) {
        $build['#attached']['library'][] = 'paragraphs_collection_demo/anchor';
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildBehaviorForm(ParagraphInterface $paragraph, array &$form, FormStateInterface $form_state) {
    $form['anchor'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Anchor'),
      '#description' => $this
        ->t('Sets an ID attribute prefixed with "scrollto-" in the Paragraph so that it can be used as a jump-to link.'),
      '#default_value' => $paragraph
        ->getBehaviorSetting($this
        ->getPluginId(), 'anchor'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary(Paragraph $paragraph) {
    $summary = [];
    if ($anchor = $paragraph
      ->getBehaviorSetting($this
      ->getPluginId(), 'anchor')) {
      $summary = [
        [
          'label' => $this
            ->t('Anchor'),
          'value' => 'scrollto-' . $anchor,
        ],
      ];
    }
    return $summary;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphsAnchorPlugin::$configFactory protected property Config factory service.
ParagraphsAnchorPlugin::buildBehaviorForm public function Builds a behavior perspective for each paragraph based on its type. Overrides ParagraphsBehaviorBase::buildBehaviorForm
ParagraphsAnchorPlugin::create public static function Overrides ParagraphsBehaviorBase::create
ParagraphsAnchorPlugin::settingsSummary public function Returns a short summary for the current behavior settings. Overrides ParagraphsBehaviorBase::settingsSummary
ParagraphsAnchorPlugin::view public function Extends the paragraph render array with behavior. Overrides ParagraphsBehaviorInterface::view
ParagraphsAnchorPlugin::__construct public function ParagraphsSliderPlugin constructor. Overrides ParagraphsBehaviorBase::__construct
ParagraphsBehaviorBase::$entityFieldManager protected property The entity field manager.
ParagraphsBehaviorBase::buildConfigurationForm public function 7
ParagraphsBehaviorBase::calculateDependencies public function
ParagraphsBehaviorBase::defaultConfiguration public function 7
ParagraphsBehaviorBase::filterBehaviorFormSubmitValues protected function Removes default behavior form values before storing the user-set ones.
ParagraphsBehaviorBase::getConfiguration public function
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 1
ParagraphsBehaviorBase::setConfiguration public function
ParagraphsBehaviorBase::settingsIcon public function Returns a short info icon for the current behavior settings. Overrides ParagraphsBehaviorInterface::settingsIcon 1
ParagraphsBehaviorBase::submitBehaviorForm public function Submit the values taken from the form to store the values. Overrides ParagraphsBehaviorInterface::submitBehaviorForm 3
ParagraphsBehaviorBase::submitConfigurationForm public function 6
ParagraphsBehaviorBase::validateBehaviorForm public function Validates the behavior fields form. Overrides ParagraphsBehaviorInterface::validateBehaviorForm 1
ParagraphsBehaviorBase::validateConfigurationForm public function 6