New answers tagged

3

As @Clive commented you need a node list cache tag. Put the slides and the cache tag when you invoke the template in the build array (not in a hook_theme(), this hook is only called when you install the module): class SliderBlock extends BlockBase { public function build() { $build = [ '#theme' => 'slider_block', '#slides' => ...


2

As mentioned on the comments by @Clive and @leymannx, you need to use cache tags to do the job. The problem is with your current approach will be tricky since your nodes are being loaded by your theme hook. Would be much cleaner and Drupal-way of doing things if you refactor your code and make your nodes loaded by the block, so your theme hook can focus on ...


2

Use a views_view_ hook, i.e. views_view_unformatted, views_view_list etc... I tried this with a PHP count for my views block and it worked great. My function is: function MYTHEME_preprocess_views_view_unformatted(&$vars) { $num_rows = $vars["rows"]; $count = count($num_rows); echo '<h2>This view has ' . $count . ' rows</h2><br&...


1

By any reason I do not really get the block rendering to respect my cache keys set wherever. It only respects set cache contexts so I create a custom cache context: In pw_basic.services.yml (pw_basic is the name ofthe module) cache_context.politician: class: Drupal\pw_basic\Cache\PoliticianCacheContext arguments: ['@current_route_match'] tags:...


2

A custom content block has no unique machine name, only a bundle type, an UUID and an ID. Only custom content blocks placed in a region have a machine name, but this is NOT the machine name of the block, it is the machine name of the block placement (configuration)


1

Custom blocks do not have machine name since they are content entities. What you can do is to check the bundle of the block in your field formatter. public function viewElements(FieldItemListInterface $items, $langcode) { if ($items->isEmpty()) { return NULL; } $entity = $items->first()->getEntity(); if ($entity->...


1

As with most things you are learning. The answer is often simpler than first thought. To resolve my issue I was able to use hook_form_alter and implement my own submission handler. From here I was able to disable the redirect to prevent the page reloading. Once redirect was disabled then my block plugin was able to get access to the form and use the ...


0

I'd do it this way: Create a custom module Create a class inside the src folder that consumes the API Create a custom block inside your module: call the class there Mostly it is better to fetch f.e. each hour and save the api data to custom entities. In this way you can cache the content in the block which enhances performance.


0

You can try the following. Add in your JS file: $(document).ajaxStart(function(){ $('#ajax_loading').show(); }); $(document).ajaxStop(function() { $('#ajax_loading').hide(); }); Add the HTML in your page: <div id="ajax_loading" style="display:none"> Add in your CSS file: #ajax_loading { position: fixed; left: 0; right: 0; top: 0; ...


1

You can create content block type using configuration files: In you module should have config/install/block_content.type.YOUR_BLOCK_TYPE_MACHINE_NAME.yml see the following example from core for Basic block core/profiles/standard/config/install/block_content.type.basic.yml . Here is an example to create new type called My Block Type Go to admin/structure/...


Top 50 recent answers are included