class HomeboxPageForm in Homebox 8
Class HomeboxPageForm.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\homebox\Form\HomeboxPageForm
Expanded class hierarchy of HomeboxPageForm
1 string reference to 'HomeboxPageForm'
File
- src/
Form/ HomeboxPageForm.php, line 23
Namespace
Drupal\homebox\FormView source
class HomeboxPageForm extends FormBase {
/**
* Current user account class.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Serializer service.
*
* @var \Symfony\Component\Serializer\Serializer
*/
protected $serializer;
/**
* HomeboxPageForm constructor.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Current user account entity.
* @param \Symfony\Component\Serializer\Serializer $serializer
* Serializer service.
*/
public function __construct(AccountInterface $account, Serializer $serializer) {
$this->account = $account;
$this->serializer = $serializer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = $container
->get('serializer');
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $container
->get('current_user');
return new static($account, $serializer);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'homebox_page';
}
/**
* Save user settings.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Symfony\Component\HttpFoundation\Request $request
* Current request.
*
* @return array
* The form structure.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public static function saveUserLayoutData(array $form, FormStateInterface &$form_state, Request $request) {
$blocks = $form_state
->getValue('blocks');
if (!is_array($blocks)) {
parse_str($form_state
->getValue('blocks'), $blocks_value);
$blocks = [];
foreach ($blocks_value as $value) {
parse_str($value, $block);
// @todo Check block deleting in js. Possible there's parent element not remove.
if ($block['id'] && $block['status']) {
$blocks[] = $block;
}
}
}
$user = \Drupal::currentUser();
$homebox = $request->attributes
->get('homebox');
$homebox_layout_storage = \Drupal::entityTypeManager()
->getStorage('homebox_layout');
$homebox_layout_id = $homebox_layout_storage
->getQuery()
->condition('type', $homebox
->id())
->condition('user_id', $user
->id())
->execute();
if (!empty($homebox_layout_id)) {
$homebox_layout = $homebox_layout_storage
->load(array_shift($homebox_layout_id));
}
else {
// Create homebox layout entity for current user.
$values = [
'user_id' => $user
->id(),
'name' => $homebox
->id(),
'status' => 1,
'type' => $homebox
->id(),
];
$homebox_layout = HomeboxLayout::create($values);
}
// Save user settings.
// @todo: use dependency injection!!!
$serializer = \Drupal::service('serializer');
$data = $serializer
->serialize($blocks, 'json');
$homebox_layout
->set('settings', $data);
$homebox_layout
->set('layout_id', $homebox
->getRegions());
$homebox_layout
->save();
return $form;
}
/**
* Ajax callback to hide block buttons.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Response to hide buttons.
*/
public static function hideBlockButtons() {
$response = new AjaxResponse();
$session = \Drupal::request()
->getSession();
$state = $session
->get('block_buttons_visible');
$response
->addCommand(new CssCommand('.block_buttons', [
'display' => $state ? 'none' : 'block',
]));
$session
->set('block_buttons_visible', !$state);
return $response;
}
/**
* Ajax callback to hide block.
*
* @param string $token
* Token of block.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Response to hide buttons.
*/
public function closeBlock($token) {
$response = new AjaxResponse();
$response
->addCommand(new RemoveCommand('#' . $token));
return $response;
}
/**
* Ajax callback to add block.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Symfony\Component\HttpFoundation\Request $request
* Current request.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* Response to hide buttons.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public static function addBlock(array $form, FormStateInterface $form_state, Request $request) {
$user = \Drupal::currentUser();
/* @var HomeboxInterface $homebox */
$homebox = $request->attributes
->get('homebox');
// Get user layout settings or default homebox settings if not exists.
/** @var \Drupal\homebox\Entity\HomeboxLayoutInterface $homebox_layout */
$homebox_layout = self::getLayoutStorageData($homebox
->id(), $user
->id());
$blocks = $homebox
->getBlocks();
if (isset($homebox_layout)) {
$serializer = \Drupal::service('serializer');
$layout_blocks = $homebox_layout
->get('settings')
->getValue();
$layout_blocks = $serializer
->decode(array_shift($layout_blocks)['value'], 'json');
}
else {
$layout_blocks = $blocks;
}
$element = $form_state
->getTriggeringElement();
foreach ($blocks as $block) {
if ($block['id'] == $element['#attributes']['block_id']) {
break;
}
$block = NULL;
}
if (!empty($block)) {
$block['status'] = TRUE;
$layout_blocks[] = $block;
/** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
$block_instance = \Drupal::service('plugin.manager.block')
->createInstance($block['id']);
$token = \Drupal::csrfToken()
->get((new Random())
->string(32));
$render[$block['id']] = [
'#type' => 'container',
'#attributes' => [
'class' => 'block_render',
'id' => $token,
],
];
$render[$block['id']]['render'] = $block_instance
->build();
$render[$block['id']]['render']['#theme'] = 'homebox_block';
$render[$block['id']]['render']['#block_id'] = $block_instance
->getPluginId();
$render[$block['id']]['render']['#block_title'] = $block['title'] != '' ? $block['title'] : $block_instance
->label();
$render[$block['id']]['render']['#block_content'] = $block_instance
->build();
$form['regions'][$block['region']][] = $render;
}
$form_state
->setValue('blocks', $layout_blocks);
self::saveUserLayoutData($form, $form_state, $request);
return $form['regions'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, HomeboxInterface $homebox = NULL) {
$is_ajax = $this
->getRequest()
->isXmlHttpRequest();
if (!$is_ajax) {
$session = \Drupal::request()
->getSession();
$session
->set('block_buttons_visible', FALSE);
}
// Get user layout settings or default homebox settings if not exists.
/** @var \Drupal\homebox\Entity\HomeboxLayoutInterface $homebox_layout */
$homebox_layout = $this
->getLayoutStorageData($homebox
->id(), $this->account
->id());
$blocks_available_for_adding = $homebox
->getBlocks();
// Check if user layout settings exists and haven't been changed.
if (isset($homebox_layout) && $homebox
->getRegions() == $homebox_layout
->get('layout_id')
->getValue()[0]['value']) {
$blocks = $homebox_layout
->get('settings')
->getValue();
$blocks = $this->serializer
->decode(array_shift($blocks)['value'], 'json');
}
else {
$blocks = $blocks_available_for_adding;
}
// Get layout regions.
/** @var \Drupal\Core\Layout\LayoutInterface $layout_instance */
$layout_instance = \Drupal::service('plugin.manager.core.layout')
->createInstance($homebox
->getRegions(), []);
$render = $regions = [];
$form['#attached']['library'][] = 'homebox/draggable-blocks';
$form['blocks'] = [
'#type' => 'hidden',
];
$form['add_block_button'] = [
'#type' => 'button',
'#value' => $this
->t('Add a block'),
'#ajax' => [
'callback' => 'Drupal\\homebox\\Form\\HomeboxPageForm::hideBlockButtons',
'event' => 'click',
],
];
$form['save_form'] = [
'#type' => 'button',
'#value' => $this
->t('Save'),
'#attributes' => [
'class' => [
'homebox-save-form',
],
],
'#ajax' => [
'callback' => 'Drupal\\homebox\\Form\\HomeboxPageForm::saveUserLayoutData',
'event' => 'click',
],
];
$form['block_buttons'] = [
'#type' => 'fieldset',
'#attributes' => [
'class' => [
'block_buttons',
],
'style' => 'display: none;',
],
];
// Create constant list of blocks available for adding on homebox page.
foreach ($blocks_available_for_adding as $block) {
/** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
$block_instance = \Drupal::service('plugin.manager.block')
->createInstance($block['id']);
$form['block_buttons']['add_block_' . $block['id']] = [
'#type' => 'button',
'#value' => isset($block['title']) && $block['title'] != '' ? $block['title'] : $block_instance
->label(),
'#attributes' => [
'block_id' => $block['id'],
],
'#ajax' => [
'callback' => 'Drupal\\homebox\\Form\\HomeboxPageForm::addBlock',
'wrapper' => 'homebox',
'event' => 'click',
],
];
}
foreach ($blocks as $block) {
$block_instance = \Drupal::service('plugin.manager.block')
->createInstance($block['id']);
if ($block['status']) {
$token = \Drupal::csrfToken()
->get((new Random())
->string(32));
$render[$block['id']] = [
'#type' => 'container',
'#attributes' => [
'id' => $token,
],
];
$render[$block['id']]['render'] = $block_instance
->build();
$render[$block['id']]['render']['#theme'] = 'homebox_block';
$render[$block['id']]['render']['#block_id'] = $block_instance
->getPluginId();
$render[$block['id']]['render']['#block_title'] = isset($block['title']) && $block['title'] != '' ? $block['title'] : $block_instance
->label();
$render[$block['id']]['render']['#block_content'] = $block_instance
->build();
$regions[$block['region']][] = $render[$block['id']];
$regions[$block['region']]['#attributes'] = [
'id' => 'region_' . $block['region'],
'class' => 'homebox-column ui-sortable',
];
}
}
$plugin_definition = $layout_instance
->getPluginDefinition();
foreach ($plugin_definition
->getRegions() as $id => $region) {
if (!isset($regions[$id])) {
$regions[$id] = [
'#markup' => '',
'#attributes' => [
'id' => 'region_' . $id,
'class' => 'homebox-column ui-sortable',
],
];
}
}
$form['regions'] = $layout_instance
->build($regions);
$form['regions']['#attributes'] = [
'id' => 'homebox',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
/**
* Helper function to get the layout storage data.
*
* @param int $homebox
* Current homebox id.
* @param int $user_id
* Current user id.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* Layout homebox of current user.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function getLayoutStorageData($homebox, $user_id) {
$homebox_layout_storage = \Drupal::entityTypeManager()
->getStorage('homebox_layout');
$homebox_layout_id = $homebox_layout_storage
->getQuery()
->condition('type', $homebox)
->condition('user_id', $user_id)
->execute();
if (!empty($homebox_layout_id)) {
$homebox_layout = $homebox_layout_storage
->load(array_shift($homebox_layout_id));
}
else {
$homebox_layout = NULL;
}
return $homebox_layout;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
HomeboxPageForm:: |
protected | property | Current user account class. | |
HomeboxPageForm:: |
protected | property | Serializer service. | |
HomeboxPageForm:: |
public static | function | Ajax callback to add block. | |
HomeboxPageForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
HomeboxPageForm:: |
public | function | Ajax callback to hide block. | |
HomeboxPageForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
HomeboxPageForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
HomeboxPageForm:: |
public static | function | Helper function to get the layout storage data. | |
HomeboxPageForm:: |
public static | function | Ajax callback to hide block buttons. | |
HomeboxPageForm:: |
public static | function | Save user settings. | |
HomeboxPageForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
HomeboxPageForm:: |
public | function | HomeboxPageForm constructor. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |