class DashboardForm in Draggable dashboard 8.2
Class DashboardForm.
@package Drupal\draggable_dashboard\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\draggable_dashboard\Form\DashboardForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of DashboardForm
File
- src/
Form/ DashboardForm.php, line 16
Namespace
Drupal\draggable_dashboard\FormView source
class DashboardForm extends EntityForm {
/**
* Block manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockPluginManager;
/**
* Context repository.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
protected $contextRepository;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->blockPluginManager = $container
->get('plugin.manager.block');
$instance->contextRepository = $container
->get('context.repository');
return $instance;
}
/**
* Sort blocks by weight.
*
* @param array $a
* @param array $b
*
* @return int
*/
protected function sortBlocks($a, $b) {
return $a['weight'] > $b['weight'] ? +1 : -1;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\draggable_dashboard\Entity\DashboardEntityInterface $dashboard */
$dashboard = $this->entity;
$form['title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $dashboard
->label(),
'#description' => $this
->t('Label for the dashboard.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#maxlength' => 64,
'#default_value' => $dashboard
->id(),
'#machine_name' => [
'exists' => [
'\\Drupal\\draggable_dashboard\\Entity\\DashboardEntity',
'load',
],
'source' => [
'title',
],
],
'#disabled' => !$dashboard
->isNew(),
];
$form['description'] = [
'#title' => $this
->t('Description'),
'#type' => 'textarea',
'#default_value' => $dashboard
->get('description'),
'#description' => '',
];
if ($dashboard
->isNew()) {
$form['columns'] = [
'#type' => 'select',
'#title' => $this
->t('Dashboard columns'),
'#options' => [
1 => $this
->t('1 Column'),
2 => $this
->t('2 Columns'),
3 => $this
->t('3 Columns'),
4 => $this
->t('4 Columns'),
],
'#default_value' => $dashboard
->get('columns'),
];
}
if (!$dashboard
->isNew()) {
$form['#attached']['library'][] = 'core/drupal.tableheader';
$form['#attached']['library'][] = 'draggable_dashboard/main';
$form['dashboard_blocks_table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Block'),
$this
->t('Region'),
$this
->t('Weight'),
[
'data' => $this
->t('Operations'),
'class' => [
'text-align-right',
],
],
],
'#empty' => $this
->t('No columns defined.'),
'#attributes' => [
'id' => 'dashboardblocks',
],
];
// Prepare available regions list.
$regionOptions = [];
for ($i = 1; $i <= $dashboard
->get('columns'); $i++) {
$regionOptions[$i] = $i . ' ' . $this
->t('Column');
}
$entities = $dashboard
->get('blocks');
// Weights range from -delta to +delta, so delta should be at least half
// of the amount of blocks present. This makes sure all blocks in the same
// region get an unique weight.
$weight_delta = round(count($entities) / 2);
for ($i = 1; $i <= $dashboard
->get('columns'); $i++) {
$region = $i;
$title = $this
->t('Column #@id', [
'@id' => $i,
]);
$form['dashboard_blocks_table']['#tabledrag'][] = [
'action' => 'match',
'relationship' => 'sibling',
'group' => 'block-region-select',
'subgroup' => 'block-region-' . $region,
'hidden' => FALSE,
];
$form['dashboard_blocks_table']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'block-weight',
'subgroup' => 'block-weight-' . $region,
];
$form['dashboard_blocks_table']['region-' . $region] = [
'#attributes' => [
'class' => [
'region-title',
'region-title-' . $region,
],
'no_striping' => TRUE,
],
];
$form['dashboard_blocks_table']['region-' . $region]['title'] = [
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'class' => 'region-title__action',
],
],
],
'#prefix' => $title,
'#type' => 'link',
'#title' => $this
->t('Place block <span class="visually-hidden">in the %region region</span>', [
'%region' => $title,
]),
'#url' => Url::fromRoute('draggable_dashboard.block_library', [
'dashboard_entity' => $dashboard
->id(),
'region' => $region,
]),
'#wrapper_attributes' => [
'colspan' => 4,
],
'#attributes' => [
'class' => [
'use-ajax',
'button',
'button--small',
'place-blocks',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
// Collect all blocks from one particular column.
$relations = [];
if (!empty($entities)) {
foreach ($entities as $key => $entity) {
if ($entity['column'] == $i) {
$relations[$key] = $entity;
}
}
uasort($relations, [
$this,
'sortBlocks',
]);
}
$form['dashboard_blocks_table']['region-' . $region . '-message'] = [
'#attributes' => [
'class' => [
'region-message',
'region-' . $region . '-message',
empty($relations) ? 'region-empty' : 'region-populated',
],
],
];
$form['dashboard_blocks_table']['region-' . $region . '-message']['message'] = [
'#markup' => '<em>' . t('No blocks in this region') . '</em>',
'#wrapper_attributes' => [
'colspan' => 4,
],
];
if (!empty($relations)) {
foreach ($relations as $key => $relation) {
/** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
$block_instance = $this->blockPluginManager
->createInstance($relation['settings']['id'], $relation['settings']);
$form['dashboard_blocks_table'][$key] = [
'#attributes' => [
'class' => [
'draggable',
],
],
];
$form['dashboard_blocks_table'][$key]['info'] = [
'#plain_text' => $block_instance
->label(),
'#wrapper_attributes' => [
'class' => [
'block',
],
],
];
$form['dashboard_blocks_table'][$key]['column'] = [
'#type' => 'select',
'#default_value' => $relation['column'],
'#required' => TRUE,
'#title' => $this
->t('Region for @block block', [
'@block' => $block_instance
->label(),
]),
'#title_display' => 'invisible',
'#options' => $regionOptions,
'#attributes' => [
'class' => [
'block-region-select',
'block-region-' . $region,
],
],
];
$form['dashboard_blocks_table'][$key]['weight'] = [
'#type' => 'weight',
'#default_value' => $relation['weight'],
'#delta' => $weight_delta,
'#title' => t('Weight for @block block', [
'@block' => $block_instance
->label(),
]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'block-weight',
'block-weight-' . $region,
],
],
];
$links = [];
$links['edit'] = [
'title' => $this
->t('Configure'),
'url' => Url::fromRoute('draggable_dashboard.block_configure', [
'block_id' => $key,
'dashboard_entity' => $dashboard
->id(),
]),
];
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('draggable_dashboard.block_delete', [
'dashboard_entity' => $dashboard
->id(),
'block_id' => $key,
]),
];
$form['dashboard_blocks_table'][$key]['operations'] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
'#wrapper_attributes' => [
'class' => [
'text-align-right',
],
],
];
}
}
}
$form['actions']['back'] = [
'#type' => 'link',
'#title' => $this
->t('Back To Dashboards'),
'#url' => new Url('entity.dashboard_entity.collection'),
'#attributes' => [
'class' => [
'button',
],
],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$blocks = $form_state
->getValue('dashboard_blocks_table', []);
if (!empty($blocks)) {
$existing_blocks = $this->entity
->get('blocks');
if (!empty($existing_blocks)) {
foreach ($existing_blocks as $block_id => $block) {
if (!empty($blocks[$block_id])) {
$existing_blocks[$block_id] = array_merge($existing_blocks[$block_id], $blocks[$block_id]);
}
}
$blocks = $existing_blocks;
}
$form_state
->setValue('blocks', $blocks);
}
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$status = $this->entity
->save();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addStatus($this
->t('Created the %label remote website.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this->entity
->toUrl('edit-form'));
break;
default:
$this
->messenger()
->addStatus($this
->t('Saved the %label remote website.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
break;
}
// Clear block plugin definitions to reflect changes in the list due to
// changes to dashboard.
$this->blockPluginManager
->clearCachedDefinitions();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DashboardForm:: |
protected | property | Block manager. | |
DashboardForm:: |
protected | property | Context repository. | |
DashboardForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
DashboardForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
DashboardForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
DashboardForm:: |
protected | function | Sort blocks by weight. | |
DashboardForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
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 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
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 |
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. |