Questions tagged [blocks]

In Drupal, a self-contained collection of content and functionality that is displayed in theme regions is called block.

Filter by
Sorted by
Tagged with
5
votes
5answers
878 views

How to stop a Views block from displaying to administrators, such as user/1?

I created a view and put an access role of anonymous user. When I log in with normal user account I don't see it, but when I log in with my admin account I see the view. I would like to not see it. ...
40
votes
9answers
83k views

How can I programmatically display a block?

I'm developing a site using Drupal 8 beta-14. I've created a view block of different terms and now I want to display it using code. How can I display it programatically? I used to do it in Drupal 7 ...
28
votes
3answers
53k views

Programatically render a block in a twig template

I need to render a views block in my page.html.twig template. In D7 I would do this: <?php $block = module_invoke('module_name', 'block_view', 'block_delta'); print render($block['content']); ?...
17
votes
6answers
14k views

How can I control block visibility with code?

In Drupal 6, I could control block visibility using PHP code in the block UI configuration like: <?php return MYMODULE_abc() || MYMODULE_def() ; ?> These functions looked up the current node, ...
18
votes
9answers
46k views

Create template file for views block

There is a lot about theming custom blocks, but what about blocks created by views? My views block has id: block-views-posts-listing-block, I try many ways for naming template file, ex: block--block-...
19
votes
1answer
29k views

Contextual filter with a block view

I have a page view("/news") with contextual filter for the topics ("news/design","news/coding"...) which are taxonomy terms and this worked fine. Due to some changes for the mobile theming, I need a ...
10
votes
6answers
18k views

How do I programmatically place a block?

From UI you can add a block in a region in that way: Go to Admin -> Structure -> Block layout (/admin/structure/block). Click on "Place block" button besides the region name. I want to do the ...
15
votes
2answers
21k views

Form within a block

I'm using drupal_get_form('event_signup_form') to embed my custom form inside a block. The form is being rendered as intended, but submitting the form triggers nothing. The form function itself is ...
29
votes
7answers
30k views

How do I correctly setup caching for my custom block showing content depending on the current node?

I have this very basic block which just shows the current node's ID. <?php /** * @file * Contains \Drupal\mymodule\Plugin\Block\ExampleEmptyBlock. */ namespace Drupal\mymodule\Plugin\Block; ...
23
votes
4answers
10k views

Are views cached through the core caching mechanisms or should I be setting the caching directly in each view?

I see there are cache settings in each of my views. Do I need to set these or does the core caching mechanisms in /admin/config/development/performance automatically do some of this and these "extra ...
14
votes
2answers
19k views

How to prevent a block from being cached?

After login. I'm adding one sidebar block. Once I clear cache then only updates variable or it's value. namespace Drupal\test_demo\Plugin\Block; use Drupal\Core\Block\BlockBase; /** * Provides '...
5
votes
4answers
4k views

How can I show a block in a view page, excluding its paginated pages?

I want to place put a block in a view page. Let's say the news page, whose path is http://www.example.com/news. The view uses pager to list the content. I want a block to show up only in the ...
2
votes
1answer
737 views

Problem setting user specific cache tags on custom block with user context

I have a simple custom block with a user cache context and a specific user specific cache tag: /** * Provides a 'User example' block. * * @Block( * id = "user_example_block", * admin_label = ...
12
votes
6answers
14k views

Place a block in multiple regions

Is there a module which can allow users to place the same block in multiple regions? That way I don't have to make the same View block twice.
26
votes
4answers
33k views

How to theme a custom block

I've created a block using hook_block_info and hook_block_theme and the like. But how can I theme it? I have it working to return an array with the keys 'subject' and 'content'. But I created the ...
9
votes
2answers
11k views

How to implement "Add another item" for custom forms?

I am writing a module which is basically a form that creates a relation between two end points. I want a user to be able to create multiple relations at once. I want to add a button which says "add ...
5
votes
2answers
3k views

Passing current node ID to the block template

I have a module with a custom block MyCustomBlock.php that get the nid of the current page and pass the value to the block template: $node = \Drupal::routeMatch()->getParameter('node'); $nid = $...
5
votes
1answer
11k views

How to display a block with menu child items programmatically?

I need to create block with a list of links menu child items (only first level) of a given menu like this: Item 1 Item 2 Item 3 With this code I can have the menu items but I don't know how to ...
16
votes
1answer
6k views

How to make a theme hook suggestion for blocks according to region?

Since Drupal 8 it is not possible with template naming conventions to load a specific block template according to the region it is placed in ("Per-region block markup and per-region block templating ...
31
votes
8answers
49k views

How do I programmatically show a view block?

I created a view block, whose machine name is "news." How do I display it using module_invoke() and the machine name of the view?
12
votes
3answers
26k views

Pass Variable to Custom Block template file

I want to create my custom block with custom content in Drupal 8,in start I create Block with just simple text on it, and work correctly, my Problem is how can I pass my custom variable to it? I saw ...
16
votes
5answers
15k views

What is the correct way to set cache contexts on custom blocks?

I have run into an issue where a block that should be unique per page isn't for logged-out users. The issue is a custom block plugin I have on a views search page that contains custom filters (sort ...
9
votes
5answers
6k views

How do I programmatically assign the access to a block?

I have created a block programmatically but I don't know how I can programmatically assign the access to it. How can I achieve it?
6
votes
1answer
7k views

Twig Template for a Custom Block Plugin

I'd like to have a custom twig template for custom block plugins from my custom module. My custom block HeaderText contains public function build() { return array( '#title' => 'Test', ...
9
votes
1answer
22k views

Pass variable to drupalSettings

I'm using Drupal 8. What I'm trying to do is: Access the active node From the node get the value of field_test and assign it to a variable Pass the variable to the drupalSettings function and ...
8
votes
1answer
13k views

How do I create a static block programmatically?

I want to create a static block using a module. I do not want to use the Views module to create a block. Could anyone explain me how to create a static block programmatically? I have already checked ...
7
votes
4answers
7k views

How do I make a block that pulls the current node content?

Ok, so I know blocks in Drupal 8 have these things called "contexts" which allow Core to clear cache on a fine-grained basis. I can't find any decent explanation of how they work, or how to get page ...
1
vote
1answer
2k views

How do I create multiple blocks?

I would like to create multiple blocks using the data rendered from the variable table in Drupal 8. I was able to achieve it in Drupal 7, but I can't find a way to do the same in Drupal 8. The code I ...
2
votes
1answer
1k views

How do I assign a region to programmatically created block?

Hi after following this guide I was able to create multiple blocks. But I have couple of issues. Blocks are not created instantly need to clear cache but cant find a way to do it instantly (solution ...
11
votes
2answers
5k views

Is there a way to set block visibility programmatically without using the PHP Filter module?

By enabling the core PHP Filter module, I can use PHP code to set the visibility of a block in the block configure page. But I don't want to enable the PHP filter module for security measure. So is ...
4
votes
5answers
2k views

Block error on CMI

I have two identical sites on Drupal 8 (A and B), on (A) I created a custom block(block/add), and then I export the configuration(drush config-export) and import this config on (B) (drush config-...
3
votes
1answer
2k views

Adding custom block problems

I've created my own module in drupal 8. I also created block in my module. I tryied to add my custom block (which is type of custom form) to view. But when I am trying to add custom block in view, its ...
3
votes
4answers
15k views

Programmatically printing a block

So far I have been using this code, which works fine to print third-party module blocks. <?php $block = module_invoke('views', 'block_view', 'og_members-block_1'); print render($block); ?&...
1
vote
2answers
4k views

Is there a way to figure out the block machine name?

In Drupal 8, is there a way to figure out what current block or view machine name you should use? For example, with the following code, I noticed that 'machine_name_to_determine' could change ...
3
votes
1answer
2k views

Attach or embed node add form to a view

I want to attach or embed a node form to a view so that when a user submits the form, the view below the form will be updated in realtime via ajax. The form will be shown above the view. My views ...
2
votes
2answers
654 views

How to show blocks by url, when nodes have multiple paths?

I know that having multiple path aliases to a single node is discouraged, but it is a situation I have to deal with. My node is accessible via /news/node-1, /baseball/news/node-1 and /basketball/news/...
2
votes
2answers
2k views

Creating QuickTabs programmatically not working

I'm trying to create some QuickTabs programmatically like it's documented on the website, but for some reason, my code bellow doesn't generate QuickTabs at all! What do I do wrong or am I looking at ...
2
votes
1answer
3k views

How to view blocks with Text Overlay on hover?

I'm working on a Drupal 7 site and I want to achieve similar thing (view blocks with Text Overlay on hover) shown here. http://ibakatv.com/ under the following blocks (sub sections): Basically I would ...
1
vote
2answers
1k views

Caching of blocks for anonymous user

I have a little probleme with my cache block. This is my build function : return [ '#markup' => time(), '#cache' => [ 'max-age' => 10 ] ]; I found this specification for block ...
0
votes
1answer
82 views

Why the content of a block created in an external module does not appear?

I am creating a module for wich i need to create a block with php code. I`m making it as follow: function my_modulo_block_info() { $blocks = array(); $blocks['start'] = array( 'title' =&...
5
votes
1answer
8k views

Ajax reload of block on an event

I have created a block through custom module. The content of the block should be random every time it loads the block. Call back function for this was written. The block also has a link say "Next" and ...
9
votes
2answers
3k views

How to assign blocks to particular regions on theme install?

Drupal 8 provides some blocks by default. Eg. Site Branding, Main Navigation, Powered by Drupal, Search etc. I have created a theme that has Site branding & Main Navigation named regions in which ...
7
votes
2answers
7k views

Place block region in node template

I am trying to place a block region inside a news content type but it is not rendering. node--news.html.twig: <article{{ attributes }} class="portal-article"> <h1 class="article-title"&...
7
votes
7answers
4k views

What's the best practice to show a node's fields in different regions?

In D7 we used to create a view for nodes, with each field having an own display. Each display creates a block to place in any region of the site. But of course, Views are SQL-heavy. Now, Drupal 8 is ...
6
votes
4answers
42k views

Insert a Views Block into a node in Drupal 7

[Drupal 7] I have a block I created in Views called "handy_tips-block_1". Now I want to insert it into the body field of my node. How do I do this? I found a guide on here, which suggests to use ...
6
votes
2answers
3k views

Block cache not clearing on save?

I have some custom block types that display in a region in my site. I have noticed that when changing the contents of these blocks, like an image, text, or title, that when you save and visit the page,...
6
votes
4answers
6k views

Remove Block Directly From Database - Drupal 7

I created a block and put in some invalid PHP code resulted in entire site not accessible at all. I'm fairly new to Drupal and I'm not a back-end programmer either. Much appreciated if you can ...
4
votes
1answer
2k views

hook_form being called twice

I have a simple form inside a block: function mymodule_block_view($delta = '') { switch($delta) { case 'some_name': $block['subject'] = t('title'); $block['content']...
4
votes
5answers
10k views

How to get a field value of custom block?

I have a custom block type, called "Features block". I added a field as alternative title (field_block_alt_title is that machine name). I would like to display this alternative title on frontpage ...
1
vote
2answers
2k views

Create a custom block programmatically and specify region and pages on which created block must shown on

For create block programmatically I use $block = BlockContent::create([ 'info' => 'block 3', 'type' => 'basic', 'langcode' => 'en', 'body' => [ 'value' => '<h1>Block'...

1
2 3 4 5 6