View source
<?php
namespace Drupal\pdb\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\pdb\FrameworkAwareBlockInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PdbBlock extends BlockBase implements FrameworkAwareBlockInterface, ContainerFactoryPluginInterface {
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public function build() {
$component = $this
->getComponentInfo();
$this->configuration['uuid'] = \Drupal::service('uuid')
->generate();
$attached = [];
$framework = $this
->attachFramework($component);
if ($framework) {
$attached = array_merge_recursive($attached, $framework);
}
$settings = $this
->attachSettings($component);
if ($settings) {
$attached = array_merge_recursive($attached, $settings);
}
$libraries = $this
->attachLibraries($component);
if ($libraries) {
$attached = array_merge_recursive($attached, $libraries);
}
$header = $this
->attachPageHeader($component);
if ($header) {
$attached = array_merge_recursive($attached, $header);
}
$contexts = $this
->getContexts();
if ($contexts) {
$contexts_values = $this
->getContextsValues($contexts);
$this->configuration['contexts'] = $contexts_values;
$js_contexts = $this
->getJsContexts($contexts_values);
$attached['drupalSettings']['pdb']['contexts'] = $js_contexts;
}
if (isset($this->configuration['pdb_configuration'])) {
$attached['drupalSettings']['pdb']['configuration'][$this->configuration['uuid']] = $this->configuration['pdb_configuration'];
}
return [
'#attached' => $attached,
];
}
public function getComponentInfo() {
$plugin_definition = $this
->getPluginDefinition();
return $plugin_definition['info'];
}
public function attachFramework(array $component) {
return [];
}
public function attachLibraries(array $component) {
$path = 'pdb/' . $component['machine_name'];
$component_libraries = [];
if (isset($component['add_css']['header']) || isset($component['add_js']['header'])) {
$component_libraries[] = $path . '/header';
}
if (isset($component['add_css']['footer']) || isset($component['add_js']['footer'])) {
$component_libraries[] = $path . '/footer';
}
return $component_libraries;
}
public function attachSettings(array $component) {
if (isset($component['settings'])) {
return [
'drupalSettings' => $component['settings'],
];
}
else {
return [];
}
}
public function attachPageHeader(array $component) {
return [];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['pdb_configuration'] = $this
->buildComponentSettingsForm($form_state);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['pdb_configuration'] = $form_state
->getValue('pdb_configuration');
}
protected function getContextsValues(array $contexts) {
$context_values = [];
foreach ($contexts as $key => $context) {
$data = $context
->getContextData();
if ($data instanceof EntityAdapter) {
$this
->getContextEntityValue($key, $data, $context_values);
}
else {
$context_values[$key] = $data
->getValue();
}
}
return $context_values;
}
protected function getContextEntityValue($key, EntityAdapter $data, array &$context_values) {
$entity_context = $data
->getValue();
if (!isset($context_values["{$key}:" . $entity_context
->getEntityTypeId()])) {
$entity = clone $entity_context;
$entity_access = $entity
->access('view', NULL, TRUE);
if (!$entity_access
->isAllowed()) {
return;
}
foreach ($entity as $field_name => $field) {
$field_access = $field
->access('view', NULL, TRUE);
if (!$field_access
->isAllowed()) {
$entity
->set($field_name, NULL);
}
}
$context_values["{$key}:" . $entity
->getEntityTypeId()] = $entity;
}
}
protected function addEntityJsContext(EntityAdapter $data, array &$js_contexts, $key) {
@trigger_error('addEntityJsContext() is deprecated in pdb:8.x-1.0 and is removed from pdb:2.0.0. Use ::getContextEntityValue() instead.', E_USER_DEPRECATED);
$this
->getContextEntityValue($key, $data, $js_contexts);
}
protected function getJsContexts(array $contexts) {
$js_contexts = [];
foreach ($contexts as $key => $context) {
if (is_object($context) && method_exists($context, 'toArray')) {
$js_contexts[$key] = $context
->toArray();
}
else {
$js_contexts[$key] = $context;
}
}
return $js_contexts;
}
protected function buildComponentSettingsForm(FormStateInterface $form_state) {
$definition = $this
->getPluginDefinition();
$elements = [];
if (isset($definition['info']['configuration'])) {
$elements = $this
->createElementsFromConfiguration($definition['info']['configuration'], $form_state);
$elements['#title'] = $this
->t('Component Settings');
$elements['#type'] = 'details';
$elements['#open'] = TRUE;
$elements['#tree'] = TRUE;
}
return $elements;
}
protected function createElementsFromConfiguration($configuration, FormStateInterface $form_state) {
$elements = [];
$defaults = !empty($this->configuration['pdb_configuration']) ? $this->configuration['pdb_configuration'] : [];
foreach ($configuration as $key => $setting) {
$element = [];
foreach ($setting as $property_key => $property) {
$element["#{$property_key}"] = $property;
}
if (isset($defaults[$key])) {
$element['#default_value'] = $this
->getElementDefaultValue($defaults[$key]);
}
$elements[$key] = $element;
}
return $elements;
}
protected function getElementDefaultValue($element_defaults) {
if (is_array($element_defaults) && isset($element_defaults['value'])) {
return $element_defaults['value'];
}
return $element_defaults;
}
}