New answers tagged

1

To sum up the comments: As a general rule, if you render the current user, then always add the correct cache metadata to the render element: public function build() { return [ '#markup' => '<span>Current User ID: ' . \Drupal::currentUser()->id() . '</span>', '#cache' => [ 'contexts' => ['user'], 'tags'...


0

The easier method to achieve this is by declaring and defining the getCacheContexts() method public function build() { $search_form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\SearchForm'); return [ 'search_form' => $search_form ]; } /** * {@inheritdoc} */ public function getCacheMaxAge() { // If you ...


1

While previewing a node, the node parameter is not "upcasted" from an integer to a node object. But you can easily do this yourself in the preprocess function, just add those lines before: $router = \Drupal::routeMatch(); $node = $router->getParameter('node'); if (is_numeric($node)) { $node = \Drupal\node\Entity\Node::load($node); } //now ...


0

Late answer for anyone who comes across this. The cfrblock module, based on renderkit, allows to combine blocks and elements in any way you want, also supports conditional logic. I am not sure if this covers the specific requirement of the original question: The original question wants to insert block-1 into block-2. What I am proposing here allows to ...


0

Late answer for anyone who comes across this. The cfrblock module, based on renderkit, allows to combine blocks and elements in any way you want, also supports conditional logic. Follow the instructions on the module page: Install cfrblock, renderkit, cfrpreset. This involves some composer packages, you will find instructions on renderkit module page. Go ...


1

You need to have some sort of content assigned to the URL url1. The simplest way would be to create a new Basic page node and set its URL alias field to url1. By the way, what you are seeing now is the "page that is displayed when no other content matches the requested document" (Administration >> Configuration >> System >> Site ...


0

Yes It is possible. Core patch is required to fix this issue. It will fix node JSON structure and attach block_content to json files. See my comment #63 for details: https://www.drupal.org/project/drupal/issues/2942975#comment-13315805


4

Use BlockContent::getInstances to get all blocks placed for this custom block, then check for each block instance if it is enabled with $block->status(). See Get block programmatically and How can we check whether block is enabled or disabled in .theme file?


0

The core block module using "getFilteredDefinitions" method to list the blocks $blockManager = \Drupal::service('plugin.manager.block'); $contextRepository = \Drupal::service('context.repository'); // Get blocks definition $definitions = $blockManager->getFilteredDefinitions('block_ui', $contextRepository->getAvailableContexts()); print_r($...


1

You need a condition plugin like RequestPath checking the host name in evaluate(): $request->getHost() and set in getCacheContexts() a context for "per site" caching: public function getCacheContexts() { $contexts = parent::getCacheContexts(); $contexts[] = 'url.site'; return $contexts; } This is a very simple module containing only one PHP file,...


0

Please have hands on the Structure Sync module. Structure sync provides Drush commands and admin interface screens for synchronizing content that could also be considered configuration. Including menu items, custom blocks and taxonomy terms. Steps: Go to structure sync. Go to Blocks tab. Export. Your configurations and content will be exported in ...


2

If this is in Drupal 8, you can export all config and search for the display id in both code and config and you should be able to find if it is being used anywhere.


1

You can use hook_form_alter do this but first should check the form concern only your block, if you check only on $form_id == 'block_form' you will hide those field in all block forms. Try with the following. /** * Implements hook_form_alter(). */ function MyModule_form_alter(&$form, $form_state, $form_id) { // Check if the provider is contact_block,...


1

Create a theme hook with the template name by replacing dashes with underscores: mymodule.module: /** * Implements hook_theme(). */ function mymodule_theme() { return [ 'block__bundle__jumbotron' => [ 'render element' => 'elements', 'base hook' => 'block', ], ]; }


0

It seems that something has changed and variables['elements']['#block'] is not available anymore. Here is what worked function ThemeName_preprocess_block(&$variables) { $block = entity_load('block', $variables['elements']['#id']); $region = $block->getRegion(); if ($region == 'nav') { $variables['attributes']['class'][] = 'SomeStyle'; } ...


0

Use ConfigEntityBase::status: if (isset($lang_block) && $lang_block->status()) { // block is enabled }


0

I think you should send render of the block not an array of block entity. View Method return an array. Try with renderer service like the following: /** * Implements hook__preprocess_HOOK(). */ function mymodule_preprocess_node(&$variables) { $node = $variables['elements']['#node']; if ($node->bundle() == 'page') { $block_entity = Drupal\...


0

You have to set "node" in the twig-variables of your block. I.e. you can do this in a template_preprocess hook like so: function mytheme_preprocess_myBlock(&$variables) { $variables['node'] = Node:load(50); } Then you can access the "node" in your template like {{node.my_field.value}}. The hard part is to find which node to load in the preprocess ...


3

You'll have to manually type it on your twig, as per https://www.drupal.org/project/twig_tweak/issues/2738781 If you don't want to type it on twig, a work around would be to add a "Text area" to your view's Header and use replacement patterns, <div class="view-title">[view:title]</div>. Alternatively, you could ditch Twig Tweak and do it ...


1

There is no hook fired in delete of block, as you can see in the list of block hooks here block.api.php. There is a topic in Drupal forum here. I think the best way to handle this is by hook_form_alter something like. /** * Implements hook_form_alter(). */ function YourModule_form_alter(&$form, $form_state, $form_id) { if ($form_id == '...


Top 50 recent answers are included