View source
<?php
namespace Drupal\gridstack\Form;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Component\Utility\Html;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\blazy\Form\BlazyAdminInterface;
use Drupal\gridstack\GridStackManagerInterface;
class GridStackAdmin implements GridStackAdminInterface {
use StringTranslationTrait;
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'] = FALSE;
$definition['grid_form'] = FALSE;
$optionsets = [];
$entities = $this->manager
->entityLoadMultiple('gridstack');
foreach ($entities as $key => $entity) {
if ($entity
->getOption('use_framework')) {
continue;
}
$optionsets[$key] = Html::escape($entity
->label());
}
$definition['optionsets'] = $optionsets;
foreach ([
'background',
'caches',
'fieldable_form',
'id',
'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');
$readme = Url::fromUri('base:' . $path . '/README.txt')
->toString();
if (!isset($form['optionset'])) {
$this->blazyAdmin
->openingForm($form, $definition);
if ($this
->manager()
->getModuleHandler()
->moduleExists('gridstack_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. Or use hook_gridstack_skins_info() and implement \\Drupal\\gridstack\\GridStackSkinInterface to register ones.', [
':url' => $readme,
]);
}
if (isset($form['background'])) {
$form['background']['#description'] = $this
->t('If trouble with image sizes not filling the given box, check this to turn the image into CSS background instead. To assign different image style per grid/box, edit the working optionset.');
}
}
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['breakpoints'])) {
$this->blazyAdmin
->breakpointsForm($form, $definition);
}
}
public function closingForm(array &$form, $definition = []) {
if (!isset($form['cache'])) {
$this->blazyAdmin
->closingForm($form, $definition);
}
$form['#attached']['library'][] = 'gridstack/admin';
}
public function getSkinOptions() {
return $this->manager
->getSkinOptions();
}
public function getLayoutOptions() {
return [
'bottom' => $this
->t('Caption bottom'),
'center' => $this
->t('Caption center'),
'top' => $this
->t('Caption top'),
];
}
public function settingsSummary($plugin, $definition = []) {
return $this->blazyAdmin
->settingsSummary($plugin, $definition);
}
public function getSettingsSummary($definition = [], $plugin = NULL) {
if (!method_exists($this->blazyAdmin, 'getSettingsSummary')) {
return $this->blazyAdmin
->settingsSummary($plugin, $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 finalizeForm(array &$form, $definition = []) {
$this->blazyAdmin
->finalizeForm($form, $definition);
}
}