<?php
namespace Drupal\paragraphs_collection_demo\Plugin\paragraphs\Behavior;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphsAnchorPlugin extends ParagraphsBehaviorBase {
protected $configFactory;
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;
}
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'));
}
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';
if ($this->configFactory
->get('paragraphs_collection_demo.settings')
->get('anchor')['show_permalink']) {
$build['#attached']['library'][] = 'paragraphs_collection_demo/anchor';
}
}
}
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;
}
public function settingsSummary(Paragraph $paragraph) {
$summary = [];
if ($anchor = $paragraph
->getBehaviorSetting($this
->getPluginId(), 'anchor')) {
$summary = [
[
'label' => $this
->t('Anchor'),
'value' => 'scrollto-' . $anchor,
],
];
}
return $summary;
}
}