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.

enter image description here

up vote 3 down vote accepted

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);
    }
  }
}

enter image description here


Answer updated to display content type restrictions.

/**
 * 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);
    }
  }
}

enter image description here

  • Hi, thanks for your answer. Actually I need to display content type assigned to the specific blocks under Description column. Can you please let me know how to achieve this by above code? – Selva mesk Oct 11 at 11:24
  • @Selvamesk – Updated answer. – leymannx Oct 11 at 12:13
  • Really you are awesome!!! Its working as I expected. Along with this, Is there any possibility to display Pages assigned to the specific blocks? – Selva mesk Oct 11 at 14:00
  • @Selvamesk – It's all there already. Simply checkout what else you find inside $conditions. – leymannx Oct 11 at 16:44
  • I have checked this and its request_path & pages but its not giving the result exactly. That's why. – Selva mesk Oct 11 at 16:59

Some general method

  • Find a unique interface string in the page and look in the core
  • Search for the URL of the page and find which routing.yml contains it. Then you will see which class handles the request

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

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.