View source
<?php
namespace Drupal\layout_paragraphs\Plugin\paragraphs\Behavior;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphsBehaviorBase;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutParagraphsPlugin extends ParagraphsBehaviorBase {
protected $layoutPluginManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManager $entity_field_manager, LayoutPluginManagerInterface $layout_plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->layoutPluginManager = $layout_plugin_manager;
}
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('plugin.manager.core.layout'));
}
public function defaultConfiguration() {
return [
'available_layouts' => [],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = $this->layoutPluginManager
->getLayoutOptions();
$available_layouts = $this->configuration['available_layouts'];
$form['available_layouts'] = [
'#title' => $this
->t('Available Layouts'),
'#type' => 'select',
'#multiple' => TRUE,
'#options' => $options,
'#default_value' => array_keys($available_layouts),
'#size' => count($options) < 8 ? count($options) * 2 : 10,
'#required' => FALSE,
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
if (empty($form_state
->getValue('available_layouts'))) {
$form_state
->setErrorByName('available_layouts', $this
->t('You must select at least one layout.'));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$available_layouts = array_filter($form_state
->getValue('available_layouts'));
foreach ($available_layouts as $layout_name) {
$layout = $this->layoutPluginManager
->getDefinition($layout_name);
$this->configuration['available_layouts'][$layout_name] = $layout
->getLabel();
}
}
public function settingsSummary(Paragraph $paragraph) {
$summary = [];
return $summary;
}
public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {
}
}