I need to add a new column called Block Short Description on Block layout under /admin/structure/block.
Can anyone let me know how to achieve it?
I have Attached screenshot for reference.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI need to add a new column called Block Short Description on Block layout under /admin/structure/block.
Can anyone let me know how to achieve it?
I have Attached screenshot for reference.
That's a form. Themed as table. So I managed to do what you want via hook_form_FORM_ID_alter
.
Thing is, I can't find the block description or admin label inside the block entity. So that's left. I hope you can find a way or maybe you can specify what "Block Short Description" is supposed to be.
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_block_admin_display_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Add header column.
array_splice($form['blocks']['#header'], 1, 0, 'Description');
// Go through all blocks.
foreach ($form['blocks'] as $bid => $block) {
// Skip non-block elements (like '#header' or 'region-name').
if ($bid[0] != '#' && !strpos($bid, '-')) {
// Load block.
$entity = \Drupal\block\Entity\Block::load($bid);
// Load description.
// ksm($entity);
// Can't find one. Take the label for now. Let OP figure this out themselves.
$description = $entity->label();
// Add description column to row.
// @see https://stackoverflow.com/a/3354804/2199525
$array = $form['blocks'][$bid];
$form['blocks'][$bid] = array_slice($array, 0, 2, TRUE) +
['description' => ['#markup' => $description]] +
array_slice($array, 2, count($array) - 1, TRUE);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_block_admin_display_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
// Add header column.
array_splice($form['blocks']['#header'], 1, 0, 'Description');
// Go through all blocks.
foreach ($form['blocks'] as $bid => $block) {
// Skip non-block elements (like '#header' or 'region-name').
if ($bid[0] != '#' && !strpos($bid, '-')) {
// Load block.
$entity = \Drupal\block\Entity\Block::load($bid);
// Load visibility settings.
$conditions = $entity->getVisibilityConditions()->getConfiguration();
// Load content types.
$types = [];
if ($node_types = isset($conditions['node_type']['bundles']) ? $conditions['node_type']['bundles'] : FALSE) {
$node_types = Drupal\node\Entity\NodeType::loadMultiple($node_types);
foreach ($node_types as $node_type) {
$types[] = $node_type->label();
}
}
// Set description.
$description = 'Content types: ';
if (!empty($types)) {
$description .= implode(', ', $types);
}
else {
$description = '- no content type restrictions -';
}
// Add description column to row.
// @see https://stackoverflow.com/a/3354804/2199525
$array = $form['blocks'][$bid];
$form['blocks'][$bid] = array_slice($array, 0, 2, TRUE) +
['description' => ['#markup' => $description]] +
array_slice($array, 2, count($array) - 1, TRUE);
}
}
}
$conditions
.
– leymannx
Oct 11 '18 at 16:44
Some general method
In your case it's the second
Route name: block.admin_display
Controller: \Drupal\block\Controller\BlockListController
which in the end is pointing to the Block entity list builder:
\Drupal\block\BlockListBuilder