Site Studio helps you to easily create a component that includes a Drupal view block so that page builders can quickly and easily drag and drop a view onto a layout. However, standard components do not include configurable filters to display specific content in the view.
This article provides information about how to create a custom element that allows you to render a view with contextual filters and expose them to the component form as configurable options.
Create a custom element by following the instruction in this user guide.
In the custom element plugin within the get fields function, define a field to hold the taxonomy term ID.
This can be a type of text field. In the example we have named the field “News type”.
You can also define other configurable options to pass to the view. For example, number of items to display per page:
public function getFields() {
return [
'news_type' => [
'title' => 'News Type',
'type' => 'textfield',
'required' => TRUE,
'validationMessage' => 'This field is required.',
],
'number_per_page' => [
'title' => 'Number items per page',
'type' => 'textfield',
'required' => TRUE,
'validationMessage' => 'This field is required.',
],
];
}In the custom element plugin, update the render function to something like the following code example:
public function render($element_settings, $element_markup, $element_class, $element_context = []) {
$view_name = news; // change as needed
$display = news_by_type; // change as needed
$view = Views::getView($view_name);
$view->setDisplay($display);
// Get the tid that was passed from the entity browser and pass it as a views argument.
$uuid = $element_settings['news_type']['entity']['#entityId'];
$entity = \Drupal::service('entity.repository')->loadEntityByUuid('taxonomy_term', $uuid);
$id = $entity->id();
$view->setArguments([$id]);
// Get the number of pages that was passed from the field and pass it as a views argument.
$view->setItemsPerPage($element_settings['number_per_page']);
$view->preExecute();
// Render the element.
return [
// update "filterable_block" to your module machine name
'#theme' => 'filterable_block_template',
'#elementSettings' => $element_settings,
'#elementMarkup' => $element_markup,
'#elementClass' => $element_class,
'#filteredData' => $view->buildRenderable(),
];
}In this example, you must update the view_name and display variables based on the machine name of the view you want to render.
In your .module file, add the filteredData variable in the return array before your hook_theme variables.
function filterable_block_theme($existing, $type, $theme, $path) {
return [
'filterable_block_template' => [
'variables' => [
'elementSettings' => NULL,
'elementMarkup' => NULL,
'elementContext' => NULL,
'elementClass' => NULL,
'filteredData' => NULL,
],
'render element' => 'children',
],
];
}Update any instances of filterable_block to the machine name of your custom element module.
Edit your twig template and add the theme variable that you set to render out the view. In this example, filteredData is used:
<div class="" >
</div>Create a component that uses the custom element that you have created.