function Block::blockForm
Same name in other branches
- 8.x-3.x modules/ctools_views/src/Plugin/Display/Block.php \Drupal\ctools_views\Plugin\Display\Block::blockForm()
Overrides Block::blockForm
File
-
modules/
ctools_views/ src/ Plugin/ Display/ Block.php, line 96
Class
- Block
- Provides a Block display plugin.
Namespace
Drupal\ctools_views\Plugin\DisplayCode
public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $form_state) {
$form = parent::blockForm($block, $form, $form_state);
$allow_settings = array_filter($this->getOption('allow'));
$block_configuration = $block->getConfiguration();
// Modify "Items per page" block settings form.
if (!empty($allow_settings['items_per_page'])) {
// Items per page.
$form['override']['items_per_page']['#type'] = 'number';
unset($form['override']['items_per_page']['#options']);
}
// Provide "Pager offset" block settings form.
if (!empty($allow_settings['offset'])) {
$form['override']['pager_offset'] = [
'#type' => 'number',
'#title' => $this->t('Pager offset'),
'#default_value' => $block_configuration['pager_offset'] ?? 0,
'#description' => $this->t('For example, set this to 3 and the first 3 items will not be displayed.'),
];
}
// Provide "Pager type" block settings form.
if (!empty($allow_settings['pager'])) {
$pager_options = [
'view' => $this->t('Inherit from view'),
'some' => $this->t('Display a specified number of items'),
'none' => $this->t('Display all items'),
];
$form['override']['pager'] = [
'#type' => 'radios',
'#title' => $this->t('Pager'),
'#options' => $pager_options,
'#default_value' => $block_configuration['pager'] ?? 'view',
];
}
// Provide "Hide fields" / "Reorder fields" block settings form.
if (!empty($allow_settings['hide_fields']) || !empty($allow_settings['sort_fields'])) {
// Set up the configuration table for hiding / sorting fields.
$fields = $this->getHandlers('field');
$header = [];
if (!empty($allow_settings['hide_fields'])) {
$header['hide'] = $this->t('Hide');
}
$header['label'] = $this->t('Label');
if (!empty($allow_settings['sort_fields'])) {
$header['weight'] = $this->t('Weight');
}
$form['override']['order_fields'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => [],
];
if (!empty($allow_settings['sort_fields'])) {
$form['override']['order_fields']['#tabledrag'] = [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'field-weight',
],
];
$form['override']['order_fields']['#attributes'] = [
'id' => 'order-fields',
];
}
// Sort available field plugins by their currently configured weight.
$sorted_fields = [];
if (!empty($allow_settings['sort_fields']) && isset($block_configuration['fields'])) {
uasort($block_configuration['fields'], '\\Drupal\\ctools_views\\Plugin\\Display\\Block::sortFieldsByWeight');
foreach (array_keys($block_configuration['fields']) as $field_name) {
if (!empty($fields[$field_name])) {
$sorted_fields[$field_name] = $fields[$field_name];
unset($fields[$field_name]);
}
}
if (!empty($fields)) {
foreach ($fields as $field_name => $field_info) {
$sorted_fields[$field_name] = $field_info;
}
}
}
else {
$sorted_fields = $fields;
}
// Add each field to the configuration table.
foreach ($sorted_fields as $field_name => $plugin) {
$field_label = $plugin->adminLabel();
if (!empty($plugin->options['label'])) {
$field_label .= ' (' . $plugin->options['label'] . ')';
}
if (!empty($allow_settings['sort_fields'])) {
$form['override']['order_fields'][$field_name]['#attributes']['class'][] = 'draggable';
}
$form['override']['order_fields'][$field_name]['#weight'] = !empty($block_configuration['fields'][$field_name]['weight']) ? $block_configuration['fields'][$field_name]['weight'] : 0;
if (!empty($allow_settings['hide_fields'])) {
$form['override']['order_fields'][$field_name]['hide'] = [
'#type' => 'checkbox',
'#default_value' => !empty($block_configuration['fields'][$field_name]['hide']) ? $block_configuration['fields'][$field_name]['hide'] : 0,
];
}
$form['override']['order_fields'][$field_name]['label'] = [
'#markup' => $field_label,
];
if (!empty($allow_settings['sort_fields'])) {
$form['override']['order_fields'][$field_name]['weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for @title', [
'@title' => $field_label,
]),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => !empty($block_configuration['fields'][$field_name]['weight']) ? $block_configuration['fields'][$field_name]['weight'] : 0,
'#attributes' => [
'class' => [
'field-weight',
],
],
];
}
}
}
// Provide "Configure filters" / "Disable filters" block settings form.
if (!empty($allow_settings['disable_filters'])) {
$items = [];
foreach ((array) $this->getOption('filters') as $filter_name => $item) {
$item['value'] = $block_configuration["filter"][$filter_name]['value'] ?? '';
$items[$filter_name] = $item;
}
$this->setOption('filters', $items);
$filters = $this->getHandlers('filter');
// Add a settings form for each exposed filter to configure or hide it.
foreach ($filters as $filter_name => $plugin) {
if ($plugin->isExposed() && ($exposed_info = $plugin->exposedInfo())) {
$form['override']['filters'][$filter_name] = [
'#type' => 'details',
'#title' => $exposed_info['label'],
];
$form['override']['filters'][$filter_name]['plugin'] = [
'#type' => 'value',
'#value' => $plugin,
];
// Render "Disable filters" settings form.
if (!empty($allow_settings['disable_filters'])) {
$form['override']['filters'][$filter_name]['disable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Disable'),
'#default_value' => !empty($block_configuration['filter'][$filter_name]['disable']) ? $block_configuration['filter'][$filter_name]['disable'] : 0,
];
}
}
}
}
// Provide "Configure sorts" block settings form.
if (!empty($allow_settings['configure_sorts'])) {
$sorts = $this->getHandlers('sort');
$options = [
'ASC' => $this->t('Sort ascending'),
'DESC' => $this->t('Sort descending'),
];
foreach ($sorts as $sort_name => $plugin) {
$form['override']['sort'][$sort_name] = [
'#type' => 'details',
'#title' => $plugin->adminLabel(),
];
$form['override']['sort'][$sort_name]['plugin'] = [
'#type' => 'value',
'#value' => $plugin,
];
$form['override']['sort'][$sort_name]['order'] = [
'#title' => $this->t('Order'),
'#type' => 'radios',
'#options' => $options,
'#default_value' => $plugin->options['order'],
];
// Set default values for sorts for this block.
if (!empty($block_configuration["sort"][$sort_name])) {
$form['override']['sort'][$sort_name]['order']['#default_value'] = $block_configuration["sort"][$sort_name];
}
}
}
return $form;
}