View source
<?php
namespace Drupal\gridstack\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\blazy\Form\BlazyAdminInterface;
use Drupal\gridstack\GridStackManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GridStackAdmin implements GridStackAdminInterface {
use StringTranslationTrait;
protected $optionsetOptions;
protected $blazyAdmin;
protected $manager;
public function __construct(BlazyAdminInterface $blazy_admin, GridStackManagerInterface $manager) {
$this->blazyAdmin = $blazy_admin;
$this->manager = $manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('blazy.admin.extended'), $container
->get('gridstack.manager'));
}
public function blazyAdmin() {
return $this->blazyAdmin;
}
public function manager() {
return $this->manager;
}
public function buildSettingsForm(array &$form, $definition = []) {
$definition['namespace'] = 'gridstack';
$definition['skins'] = $this
->getSkinOptions();
$definition['style'] = !empty($definition['style']);
$definition['grid_form'] = !empty($definition['grid_form']);
$definition['optionsets'] = $this
->getOptionsetsByGroupOptions('js');
foreach ([
'background',
'caches',
'fieldable_form',
'vanilla',
] as $key) {
$definition[$key] = isset($definition[$key]) ? $definition[$key] : TRUE;
}
$definition['layouts'] = isset($definition['layouts']) ? array_merge($this
->getLayoutOptions(), $definition['layouts']) : $this
->getLayoutOptions();
$this
->openingForm($form, $definition);
$this
->mainForm($form, $definition);
$this
->closingForm($form, $definition);
}
public function openingForm(array &$form, &$definition = []) {
$path = drupal_get_path('module', 'gridstack');
$is_ui = $this
->manager()
->getModuleHandler()
->moduleExists('gridstack_ui');
$is_help = $this
->manager()
->getModuleHandler()
->moduleExists('help');
$route = [
'name' => 'gridstack_ui',
];
$readme = $is_ui && $is_help ? Url::fromRoute('help.page', $route)
->toString() : Url::fromUri('base:' . $path . '/README.md')
->toString();
if (!isset($form['optionset'])) {
$this->blazyAdmin
->openingForm($form, $definition);
if ($is_ui) {
$route_name = 'entity.gridstack.collection';
$form['optionset']['#description'] = $this
->t('Manage optionsets at <a href=":url" target="_blank">the optionset admin page</a>.', [
':url' => Url::fromRoute($route_name)
->toString(),
]);
}
}
if (isset($form['skin'])) {
$form['skin']['#description'] = $this
->t('Skins allow various layouts with just CSS. Some options below depend on a skin. Leave empty to DIY. Check out <a href=":url">docs to register skins</a>.', [
':url' => $readme,
]);
}
$form['gridnative'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use native CSS Grid'),
'#description' => $this
->t('<b>Experimental!</b> Check to replace any js-driven (gridstack, masonry, packery, isotope) layouts with native browser Grid layout. Check out <a href=":url">CSS Grid browser supports</a> relevant to your visitors. <br>Uncheck if any issue.', [
':url' => 'https://caniuse.com/#feat=css-grid',
]),
];
if (isset($form['background'])) {
$form['background']['#description'] = $this
->t('Only if troubled with image sizes not making the given box gapless, check this to turn the image into CSS background instead. Blazy formatter only.');
$form['background']['#weight'] = -40;
}
}
public function mainForm(array &$form, $definition = []) {
if (!empty($definition['image_style_form'])) {
$this->blazyAdmin
->imageStyleForm($form, $definition);
}
if (!empty($definition['media_switch_form'])) {
$this->blazyAdmin
->mediaSwitchForm($form, $definition);
}
if (!empty($definition['fieldable_form'])) {
$this->blazyAdmin
->fieldableForm($form, $definition);
if (!empty($definition['links'])) {
$form['category'] = [
'#title' => $this
->t('Category'),
'#type' => 'select',
'#options' => $definition['links'],
'#description' => $this
->t('The category to display inside the box.'),
];
}
}
if (!empty($definition['stamps'])) {
$form['stamp'] = [
'#title' => $this
->t('Stamp'),
'#type' => 'select',
'#options' => $definition['stamps'],
'#description' => $this
->t('Stamp is _just a unique list, <b>Html list</b>, such as Latest news, blogs, testimonials, etc. replacing one of the other boring boxes, including rich ones. Leave empty to not use stamp.'),
'#weight' => -67,
];
$form['stamp_index'] = [
'#title' => $this
->t('Stamp index'),
'#type' => 'textfield',
'#description' => $this
->t('Which index, a stamp should be inserted into.'),
'#weight' => -66,
];
}
if (isset($form['overlay'])) {
$form['overlay']['#title'] = $this
->t('Rich box');
$form['overlay']['#description'] = $this
->t('Replace <b>Main stage/ image</b> if a node has one. It can be any entity reference, like Block, etc. Use block_field.module for ease of block additions. How? Create a sticky (or far future created) node or two containing a Slick carousel, video, weather, time, donations, currency, ads, or anything as a block field, and put it here. Be sure different from the <b>Main stage</b>. While regular boxes are updated with new contents, these rich boxes may stay the same, and sticky (Use Views with sort by sticky or desc by creation).');
$form['overlay']['#weight'] = -65;
}
if (!empty($definition['breakpoints'])) {
$this->blazyAdmin
->breakpointsForm($form, $definition);
}
}
public function closingForm(array &$form, $definition = []) {
if (!isset($form['cache'])) {
$this->blazyAdmin
->closingForm($form, $definition);
}
if (isset($form['cache'])) {
$form['cache']['#weight'] = empty($definition['_views']) ? -40 : -22;
}
$form['#attached']['library'][] = 'gridstack/admin';
}
public function getOptionsetsByGroupOptions($group = '') {
if (!isset($this->optionsetOptions[$group])) {
$optionsets = [];
foreach ($this->manager
->entityLoadMultiple('gridstack') as $key => $entity) {
if ($group && $group == 'js' && $entity
->getOption('use_framework')) {
continue;
}
$optionsets[$key] = Html::escape($entity
->label());
}
ksort($optionsets);
$this->optionsetOptions[$group] = $optionsets;
}
return $this->optionsetOptions[$group];
}
public function getSkinOptions() {
return $this->manager
->skinManager()
->getSkinOptions();
}
public function getLayoutOptions() {
return [
'bottom' => $this
->t('Caption bottom'),
'center' => $this
->t('Caption center'),
'top' => $this
->t('Caption top'),
];
}
public function getSettingsSummary($definition = []) {
return $this->blazyAdmin
->getSettingsSummary($definition);
}
public function getFieldOptions($target_bundles = [], $allowed_field_types = [], $entity_type_id = 'media', $target_type = '') {
return $this->blazyAdmin
->getFieldOptions($target_bundles, $allowed_field_types, $entity_type_id, $target_type);
}
public function getResponsiveImageOptions() {
return $this->blazyAdmin
->getResponsiveImageOptions();
}
public function finalizeForm(array &$form, $definition = []) {
$this->blazyAdmin
->finalizeForm($form, $definition);
}
}