View source
<?php
namespace Drupal\panels_ipe\Plugin\DisplayBuilder;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\layout_plugin\Plugin\Layout\LayoutInterface;
use Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\panels\Storage\PanelsStorageManagerInterface;
use Drupal\user\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InPlaceEditorDisplayBuilder extends StandardDisplayBuilder {
protected $tempStore;
protected $panelsStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextHandlerInterface $context_handler, AccountInterface $account, SharedTempStoreFactory $temp_store_factory, PanelsStorageManagerInterface $panels_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $context_handler, $account);
$this->tempStore = $temp_store_factory
->get('panels_ipe');
$this->panelsStorage = $panels_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.handler'), $container
->get('current_user'), $container
->get('user.shared_tempstore'), $container
->get('panels.storage_manager'));
}
protected function getDrupalSettings(array $regions, LayoutInterface $layout, PanelsDisplayVariant $panels_display, $unsaved) {
$settings = [
'regions' => [],
];
foreach ($regions as $region => $blocks) {
$settings['regions'][$region] = [
'name' => $region,
'label' => '',
'blocks' => [],
];
if (!$blocks) {
continue;
}
foreach ($blocks as $block_uuid => $block) {
$configuration = $block
->getConfiguration();
$plugin_definition = $block
->getPluginDefinition();
$setting = [
'uuid' => $block_uuid,
'label' => $block
->label(),
'id' => $block
->getPluginId(),
'provider' => $configuration['provider'],
'plugin_id' => $plugin_definition['id'],
];
$settings['regions'][$region]['blocks'][$block_uuid] = $setting;
}
}
$storage_type = $panels_display
->getStorageType();
$storage_id = $panels_display
->getStorageId();
$layout_definition = $layout
->getPluginDefinition();
$settings['layout'] = [
'id' => $layout
->getPluginId(),
'label' => $layout_definition['label'],
'original' => TRUE,
];
$settings['user_permission'] = [
'change_layout' => $this->panelsStorage
->access($storage_type, $storage_id, 'change layout', $this->account)
->isAllowed(),
'create_content' => $this->account
->hasPermission('administer blocks'),
];
$settings['panels_display'] = [
'storage_type' => $storage_type,
'storage_id' => $storage_id,
'id' => $panels_display
->id(),
];
$settings['unsaved'] = $unsaved;
return $settings;
}
public function build(PanelsDisplayVariant $panels_display) {
$has_permission = $this->account
->hasPermission('access panels in-place editing') && $this->panelsStorage
->access($panels_display
->getStorageType(), $panels_display
->getStorageId(), 'update', $this->account)
->isAllowed();
if ($has_permission) {
$has_permission = \Drupal::service('plugin.manager.ipe_access')
->access($panels_display);
}
if ($has_permission) {
$unsaved = FALSE;
$temp_store_key = $panels_display
->id();
if ($variant_config = $this->tempStore
->get($temp_store_key)) {
unset($variant_config['id']);
$panels_display
->setConfiguration($variant_config);
$unsaved = TRUE;
}
$build = parent::build($panels_display);
$regions = $panels_display
->getRegionAssignments();
$layout = $panels_display
->getLayout();
foreach ($regions as $region => $blocks) {
$region_name = Html::getClass("block-region-{$region}");
$build[$region]['#prefix'] = '<div class="' . $region_name . '" data-region-name="' . $region . '">';
$build[$region]['#suffix'] = '</div>';
if ($blocks) {
foreach ($blocks as $block_id => $block) {
$build[$region][$block_id]['#attributes']['data-block-id'] = $block_id;
}
}
}
$build['#attached']['library'][] = 'panels_ipe/panels_ipe';
$build['#attached']['drupalSettings']['panels_ipe'] = $this
->getDrupalSettings($regions, $layout, $panels_display, $unsaved);
$build['#prefix'] = '<div id="panels-ipe-content">';
$build['#suffix'] = '</div><div id="panels-ipe-tray"></div>';
}
else {
$build = parent::build($panels_display);
}
return $build;
}
}