View source
<?php
namespace Drupal\gridstack;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\blazy\Blazy;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GridStackHook {
use StringTranslationTrait;
protected $manager;
public function __construct(GridStackManagerInterface $manager) {
$this->manager = $manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('gridstack.manager'));
}
public function configSchemaInfoAlter(array &$definitions) {
if (isset($definitions['layout_plugin.settings'])) {
$this
->mapConfigSchemaInfoAlter($definitions['layout_plugin.settings']);
}
if (isset($definitions['core.entity_view_display.*.*.*.third_party.ds'])) {
$this
->mapConfigSchemaInfoAlter($definitions['core.entity_view_display.*.*.*.third_party.ds']['mapping']['layout']['mapping']['settings']);
}
foreach ([
'gridstack_base',
'gridstack_vanilla',
] as $key) {
if (isset($definitions[$key])) {
Blazy::configSchemaInfoAlter($definitions, $key, GridStackDefault::extendedSettings());
}
}
}
public function mapConfigSchemaInfoAlter(array &$mappings, $source = '') {
foreach (GridStackDefault::layoutSettings() as $key => $value) {
if (is_array($value)) {
$mappings['mapping'][$key]['type'] = 'sequence';
$mappings['mapping'][$key]['label'] = $key;
$mappings['mapping'][$key]['sequence'][0]['type'] = 'mapping';
$mappings['mapping'][$key]['sequence'][0]['label'] = $key;
}
else {
$mappings['mapping'][$key]['type'] = 'string';
$mappings['mapping'][$key]['label'] = ucwords($key);
}
}
foreach (GridStackDefault::regionSettings() as $key => $value) {
if (is_array($value)) {
$mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['type'] = 'sequence';
$mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['label'] = $key;
$mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['sequence'][0]['type'] = 'mapping';
$mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['sequence'][0]['label'] = $key;
}
else {
$mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['type'] = 'string';
$mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['label'] = ucwords($key);
}
}
}
public function libraryInfoAlter(&$libraries, $extension) {
if ($path = \gridstack_libraries_get_path('gridstack')) {
$libraries['gridstack']['js'] = [
'/' . $path . '/dist/gridstack.min.js' => [
'weight' => -2,
],
];
}
if (isset($libraries['backbone'])) {
$libraries['backbone']['dependencies'][] = $this->manager
->configLoad('dev', 'gridstack.settings') ? 'gridstack/dev' : 'gridstack/all';
}
if (isset($libraries['load'])) {
$libraries['load']['dependencies'][] = $this->manager
->configLoad('gridstatic', 'gridstack.settings') ? 'gridstack/gridstatic' : 'gridstack/gridstack';
}
}
public function fieldFormatterInfoAlter(array &$info) {
$common = [
'quickedit' => [
'editor' => 'disabled',
],
'provider' => 'gridstack',
];
if ($this->manager
->getModuleHandler()
->moduleExists('video_embed_media')) {
$info['gridstack_file'] = $common + [
'id' => 'gridstack_file',
'label' => $this
->t('GridStack Image with VEF (deprecated)'),
'description' => $this
->t('Display the images associated to VEM/ME as a simple mix of GridStack image/video (deprecated for GridStack Media).'),
'class' => 'Drupal\\gridstack\\Plugin\\Field\\FieldFormatter\\GridStackFileFormatter',
'field_types' => [
'entity_reference',
'image',
],
];
}
if ($this->manager
->getModuleHandler()
->moduleExists('paragraphs')) {
$info['gridstack_paragraphs'] = $common + [
'id' => 'gridstack_paragraphs',
'label' => $this
->t('GridStack Paragraphs'),
'description' => $this
->t('Display the Paragraphs as a GridStack.'),
'class' => 'Drupal\\gridstack\\Plugin\\Field\\FieldFormatter\\GridStackParagraphsFormatter',
'field_types' => [
'entity_reference_revisions',
],
];
}
}
public function layoutAlter(&$definitions) {
$optionsets = $this->manager
->entityLoadMultiple('gridstack');
$excluded = $this->manager
->configLoad('excludes', 'gridstack.settings');
$framework = $this->manager
->configLoad('framework', 'gridstack.settings');
$path = \drupal_get_path('module', 'gridstack');
$excludes = [
'default',
];
if (!empty($excluded)) {
$excludes = array_unique(array_merge($excludes, array_map('trim', explode(",", $excluded))));
}
foreach ($optionsets as $key => $optionset) {
if (in_array($key, $excludes)) {
continue;
}
$static = !empty($framework) && $optionset
->getOption('use_framework');
$id = $optionset
->id();
$layout_id = GridStackDefault::layoutId($id);
$regions = $optionset
->prepareRegions();
$definition = [
'label' => $optionset
->label(),
'category' => $static ? 'GridStack ' . ucwords($framework) : 'GridStack JS',
'class' => '\\Drupal\\gridstack\\Plugin\\Layout\\GridStackLayout',
'default_region' => 'gridstack_0',
'icon' => $optionset
->getIconUrl(),
'id' => $layout_id,
'provider' => 'gridstack',
'additional' => [
'optionset' => $id,
],
'regions' => $regions,
'theme_hook' => 'gridstack',
'path' => $path,
'library' => 'gridstack/layout',
'config_dependencies' => [
'config' => [
'gridstack.optionset.' . $id,
],
'module' => [
'gridstack',
],
],
];
$definitions[$layout_id] = new LayoutDefinition($definition);
}
}
}