Is there a module or another way to add custom fields to a Views block, where the field would be filled in when placing the block in the Block layout, similar to how "Items per block" is selected?
For example, a Background Color field where one of the predefined colors could be chosen.

Background color field when inserting Views block in Block layout

Maybe these fields should be entered into views under "Block settings" or in some other way:

Background color field in Views block settings

Comments

ressa’s picture

I usually do this by adding a class in the View, or in the block with https://www.drupal.org/project/block_class.

drale01’s picture

Yes, I have considered that, but how would I set up something more complex, like a responsive background image? In that case, it would have to be a field.
Or multiple different fields, for example:

  • Block height;
  • Different item alignment.
wombatbuddy’s picture

What about to use a content block and render a view in it with the module like Viewfield?

drale01’s picture

You are right, that is one way to solve it, but it complicates things for the client since I’m using Mercury Editor. With Viewfield, the client would:

  • First, need to create a block containing the view and additional fields.
  • Then, insert that block onto the page via Mercury Editor.

For each different block configuration, they would have to create a new block, which makes this approach a bit complicated.

Is there a way to insert the fields below "Items per block"
 

ressa’s picture

I think it would be easier for us to help you, if you made a use case, like "As en editor with these permissions, I want to be able to ... etc."

drale01’s picture

Thank you for your input! It helped me find a solution.

Instead of using Viewfield inside a Block, I will use it within Paragraphs.

Use Case:

As an editor with the necessary permissions, I want to have the flexibility to modify the appearance and structure of a Lists (Views) block without needing to create multiple blocks or manually override configurations each time.

Implementation:

I am building a theme that allows clients to make simple modifications to Lists (Views) blocks. Instead of relying on Viewfield in a Block, I have integrated it within Paragraphs, which offers a more user-friendly and reusable approach.

With this setup, the editor can:

  • Use a Selection List field to choose whether the list items should be aligned to the left or right.
  • Use a Media field to select a background image that will be applied behind all list items.

All of these modifications are dynamically applied through a Twig template, making it easier for the client to adjust settings without creating new blocks every time they need a small variation.

This approach keeps the editing process more streamlined while leveraging the flexibility of Paragraphs.

Thanks again for the discussion—it helped me refine the solution!

ressa’s picture

Fantastic, thanks for sharing your use case and solution, great that you solved this task!

I have begun using use cases in issues myself, to make it clearer what is in play, like in #3504766: Allow defining a base URL. Not only does it help others to more easily understand my motivation, but it also helps in making it clearer to myself what should be taken into consideration.

jaypan’s picture

I just ran into this exact issue today, and I was not able to find a way to add/extend configuration to a views block. In the end, I went an alternate route - I created a block plugin, that has custom configuration, and renders the view.

Use case:

  1. I have a views block displaying items
  2. This block will be rendered in multiple areas on the site
  3. The header text for the block must be customizable in each area it is rendered in on the site
  4. For a clean UX, the header text must be entered when placing the block, ie in layout builder or on the block layout page (or anywhere else).

Solution:

<?php

declare(strict_types=1);

namespace Drupal\herons_base\Plugin\Block;

use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Creates a 
 */
#[Block(
  id: "abc_list_block",
  admin_label: new TranslatableMarkup("A listing of ABC block items"),
)]
class AbcBlocksListBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */  
  public function defaultConfiguration() {
    return [
      'header_text' => [
        'value' => NULL,
        'format' => NULL,
      ],
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $header_text = this->configuration['header_text'];
    $form['header_text'] = [
      '#type' => 'text_format',
      '#title' => $this->t('Header Text'),
      '#default_value' => $header_text['value'],
      '#format' => $header_text['format'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['hello_block_name'] = $form_state->getValue('header_text');
  }


  /**
   * {@inheritdoc}
   */
  public function build() {
    $header_text = this->configuration['header_text'];

    return [
      '#prefix' => '<div class="abc_list_wrapper">',
      '#suffix' => '</div>',
      'header' [
        '#prefix' => '<div class="header-text">',
        '#suffix' => '</div>',
        '#type' => 'processed_text',
        '#text' => $header_text['value'],
        '#format' => $header_text['format'],
      ],
      'view' => [
        '#type' => 'view',
        '#name' => 'abc_blocks',
        '#display_id' => 'listing_block',
      ],
    ];
  }

}

With this solution, the CKeditor enabled 'header text' field is made available on layout builder, and the block admin page, when placing a block, and renders the header when the block is rendered.

Contact me to contract me for D7 -> D10/11 migrations.

wombatbuddy’s picture

@jaypan, thank you for sharing your clever solution!

ressa’s picture

Thanks for sharing also @jaypan. It's in forum posts such as this, that images could be really useful, to demonstrate how the result looks like for the end user, in the GUI.

I am still hoping #2926532: Provide file upload for forums will happen.