class PanelsContentForm in Panels 8.3
Same name and namespace in other branches
- 8.4 src/Form/PanelsContentForm.php \Drupal\panels\Form\PanelsContentForm
Provides a form for editing a panel variant display's content.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\panels\Form\PanelsContentForm uses AjaxFormTrait
Expanded class hierarchy of PanelsContentForm
1 file declares its use of PanelsContentForm
- StandardDisplayBuilder.php in src/
Plugin/ DisplayBuilder/ StandardDisplayBuilder.php
File
- src/
Form/ PanelsContentForm.php, line 15
Namespace
Drupal\panels\FormView source
class PanelsContentForm extends FormBase {
use AjaxFormTrait;
/**
* Tempstore factory.
*
* @var \Drupal\user\SharedTempStoreFactory
*/
protected $tempstore;
/**
* The tempstore ID.
*
* @var string
*/
protected $tempstore_id;
/**
* Constructs a new VariantPluginContentForm.
*
* @param \Drupal\user\SharedTempStoreFactory $tempstore
* The tempstore factory.
*/
public function __construct(SharedTempStoreFactory $tempstore) {
$this->tempstore = $tempstore;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('user.shared_tempstore'));
}
/**
* Get the tempstore ID.
*
* @return string
*/
protected function getTempstoreId() {
return $this->tempstore_id;
}
/**
* Get the tempstore.
*
* @return \Drupal\user\SharedTempStore
*/
protected function getTempstore() {
return $this->tempstore
->get($this
->getTempstoreId());
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'panels_block_page_content';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'block/drupal.block';
$this->tempstore_id = $form_state
->getFormObject()
->getTempstoreId();
$cached_values = $form_state
->getTemporaryValue('wizard');
/** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */
$variant_plugin = $cached_values['plugin'];
// Allow to configure the page title, even when adding a new display.
// Default to the page label in that case.
$form['page_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Page title'),
'#description' => $this
->t('Configure the page title that will be used for this display.'),
'#default_value' => $variant_plugin
->getConfiguration()['page_title'] ?: '',
];
$pattern_plugin = $variant_plugin
->getPattern();
$machine_name = $pattern_plugin
->getMachineName($cached_values);
// Set up the attributes used by a modal to prevent duplication later.
$attributes = $this
->getAjaxAttributes();
$add_button_attributes = $this
->getAjaxButtonAttributes();
if ($block_assignments = $variant_plugin
->getRegionAssignments()) {
// Build a table of all blocks used by this variant.
$form['add'] = [
'#type' => 'link',
'#title' => $this
->t('Add new block'),
'#url' => $pattern_plugin
->getBlockListUrl($this->tempstore_id, $machine_name, NULL, $this
->getRequest()
->getRequestUri()),
'#attributes' => $add_button_attributes,
'#attached' => [
'library' => [
'core/drupal.ajax',
],
],
];
$form['blocks'] = [
'#type' => 'table',
'#header' => [
$this
->t('Label'),
$this
->t('Plugin ID'),
$this
->t('Region'),
$this
->t('Weight'),
$this
->t('Operations'),
],
'#attributes' => array(
'id' => 'blocks',
),
'#empty' => $this
->t('There are no regions for blocks.'),
];
// Loop through the blocks per region.
foreach ($block_assignments as $region => $blocks) {
// Add a section for each region and allow blocks to be dragged between
// them.
$form['blocks']['#tabledrag'][] = [
'action' => 'match',
'relationship' => 'sibling',
'group' => 'block-region-select',
'subgroup' => 'block-region-' . $region,
'hidden' => FALSE,
];
$form['blocks']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'block-weight',
'subgroup' => 'block-weight-' . $region,
];
$form['blocks']['region-' . $region] = [
'#attributes' => [
'class' => [
'region-title',
'region-title-' . $region,
],
'no_striping' => TRUE,
],
];
$form['blocks']['region-' . $region]['title'] = [
'#markup' => $variant_plugin
->getRegionName($region),
'#wrapper_attributes' => [
'colspan' => 5,
],
];
$form['blocks']['region-' . $region . '-message'] = [
'#attributes' => [
'class' => [
'region-message',
'region-' . $region . '-message',
empty($blocks) ? 'region-empty' : 'region-populated',
],
],
];
$form['blocks']['region-' . $region . '-message']['message'] = [
'#markup' => '<em>' . $this
->t('No blocks in this region') . '</em>',
'#wrapper_attributes' => [
'colspan' => 5,
],
];
/** @var \Drupal\Core\Block\BlockPluginInterface[] $blocks */
foreach ($blocks as $block_id => $block) {
$row = [
'#attributes' => [
'class' => [
'draggable',
],
],
];
$row['label']['#markup'] = $block
->label();
$row['id']['#markup'] = $block
->getPluginId();
// Allow the region to be changed for each block.
$row['region'] = [
'#title' => $this
->t('Region'),
'#title_display' => 'invisible',
'#type' => 'select',
'#options' => $variant_plugin
->getRegionNames(),
'#default_value' => $variant_plugin
->getRegionAssignment($block_id),
'#attributes' => [
'class' => [
'block-region-select',
'block-region-' . $region,
],
],
];
// Allow the weight to be changed for each block.
$configuration = $block
->getConfiguration();
$row['weight'] = [
'#type' => 'weight',
'#default_value' => isset($configuration['weight']) ? $configuration['weight'] : 0,
'#title' => $this
->t('Weight for @block block', [
'@block' => $block
->label(),
]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'block-weight',
'block-weight-' . $region,
],
],
];
// Add the operation links.
$operations = [];
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => $pattern_plugin
->getBlockEditUrl($this->tempstore_id, $machine_name, $block_id, $this
->getRequest()
->getRequestUri()),
'attributes' => $attributes,
];
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => $pattern_plugin
->getBlockDeleteUrl($this->tempstore_id, $machine_name, $block_id, $this
->getRequest()
->getRequestUri()),
'attributes' => $attributes,
];
$row['operations'] = [
'#type' => 'operations',
'#links' => $operations,
];
$form['blocks'][$block_id] = $row;
}
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
/** @var \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant $variant_plugin */
$variant_plugin = $cached_values['plugin'];
// If the blocks were rearranged, update their values.
if (!$form_state
->isValueEmpty('blocks')) {
foreach ($form_state
->getValue('blocks') as $block_id => $block_values) {
$variant_plugin
->updateBlock($block_id, $block_values);
}
}
// Page Variant title handling.
if ($form_state
->hasValue('page_title')) {
$configuration = $variant_plugin
->getConfiguration();
$configuration['page_title'] = $form_state
->getValue('page_title');
$variant_plugin
->setConfiguration($configuration);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxFormTrait:: |
public static | function | Gets attributes for use with an AJAX modal. | |
AjaxFormTrait:: |
public static | function | Gets attributes for use with an add button AJAX modal. | |
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 |
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. | |
PanelsContentForm:: |
protected | property | Tempstore factory. | |
PanelsContentForm:: |
protected | property | The tempstore ID. | |
PanelsContentForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
PanelsContentForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
PanelsContentForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
PanelsContentForm:: |
protected | function | Get the tempstore. | |
PanelsContentForm:: |
protected | function | Get the tempstore ID. | |
PanelsContentForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
PanelsContentForm:: |
public | function | Constructs a new VariantPluginContentForm. | |
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. |