View source
<?php
namespace Drupal\homebox\Form;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Layout\LayoutPluginManager;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HomeboxForm extends EntityForm {
protected $manager;
protected $layoutPluginManager;
protected $blockManager;
protected $contextRepository;
protected $pluginCacheClearer;
protected $cacheRender;
public function __construct(LayoutPluginManager $layoutPluginManager, BlockManagerInterface $block_manager, ContextRepositoryInterface $context_repository, CacheBackendInterface $cacheRender, CachedDiscoveryClearerInterface $plugin_cache_clearer) {
$this->layoutPluginManager = $layoutPluginManager;
$this->blockManager = $block_manager;
$this->contextRepository = $context_repository;
$this->cacheRender = $cacheRender;
$this->pluginCacheClearer = $plugin_cache_clearer;
}
public static function create(ContainerInterface $container) {
$plugin_cache_clearer = $container
->get('plugin.cache_clearer');
$cache_render = $container
->get('cache.render');
$context_repository = $container
->get('context.repository');
$block_manager = $container
->get('plugin.manager.block');
$layout_plugin_manager = $container
->get('plugin.manager.core.layout');
return new static($layout_plugin_manager, $block_manager, $context_repository, $cache_render, $plugin_cache_clearer);
}
protected function getLayout($layout_id, array $layout_settings, FormStateInterface $form_state) {
if (!($layout_plugin = $form_state
->get('layout_plugin'))) {
$layout_plugin = $this->layoutPluginManager
->createInstance($layout_id, $layout_settings);
$form_state
->set('layout_plugin', $layout_plugin);
}
return $layout_plugin;
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$homebox = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $homebox
->label(),
'#description' => $this
->t("Label for the Homebox."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $homebox
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\homebox\\Entity\\Homebox::load',
],
'#disabled' => !$homebox
->isNew(),
];
$form['path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path'),
'#maxlength' => 255,
'#default_value' => $homebox
->getPath(),
'#description' => $this
->t("Specify a URL by which this page can be accessed. For example, type \"dashboard\" when creating a Dashboard page. Use a relative path and don't add a trailing slash or the URL alias won't work."),
'#required' => TRUE,
];
$form['options'] = [
'#type' => 'checkboxes',
'#options' => [
'menu' => t('Visible menu item'),
'status' => t('Enable the page'),
],
];
$roles = user_roles();
$role_list = [];
foreach ($roles as $id => $role) {
if (!$role
->isAdmin()) {
$role_list[$role
->id()] = $role
->label();
}
}
$form['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allow only certain roles to access the page'),
'#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $role_list),
'#description' => $this
->t('Select which roles can view the page.'),
];
$form['columns'] = [
'#type' => 'select',
'#title' => t('Number of columns'),
'#options' => $this->layoutPluginManager
->getLayoutOptions(),
'#description' => t('Set the number of columns you want to activate for this Homebox page.'),
'#default_value' => $homebox
->getRegions(),
];
if (!$homebox
->isNew()) {
$form['options']['#default_value'] = $homebox
->getOptions();
$form['roles']['#default_value'] = $homebox
->getRoles();
}
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$homebox = $this->entity;
$homebox
->setBlocks([]);
$status = $homebox
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addMessage($this
->t('Created the %label Homebox.', [
'%label' => $homebox
->label(),
]));
break;
default:
$this
->messenger()
->addMessage($this
->t('Saved the %label Homebox.', [
'%label' => $homebox
->label(),
]));
}
$this->pluginCacheClearer
->clearCachedDefinitions();
$this->cacheRender
->invalidateAll();
$form_state
->setRedirectUrl($homebox
->toUrl('collection'));
}
}