Drupal 7 hook block title from views module - drupal

I have views that shows profiles of my site within a block.
Now, I am trying to alter the title of that block through hook_block_view_MODULE_DELTA_alter.
here is the code of my custom module:
<?php
function homepagefilter_block_view_views_new_users_alter(&$data, $block) {
$data['subject'] = t('New title of the block');
}
I doen't see any effect on the block title...
Why is that?

try hook_block_view_alter().
function homepagefilter_block_view_alter(&$data, $block){
if($block->delta == 'BLOCKID'){
print_r($block);
print_r($data['subject']);
}
}

The deltas of blocks created by the Views module looks like "[view_name]-[display_name]", and the display name is something like "[type]_[number]" by default. So that means the full delta for your block is probably something like "new_users-block_1". However, you can't implement homepagefilter_block_view_views_new_users-block_1_alter(), because you can't use a hyphen in a function name. The blocks Menu module creates has a similar problem (see this issue). As Behzad says, you'll have to implement the generic hook_block_view_alter() hook for now.

Related

Drupal 8: Inserting a block/view into the menu twig template

Is it possible to have a block/view render from within my Mega Menu twig template?
I've created a region, created a view/block and added that block to the region. But it's a matter of having that content from the view displayed in my menu.
I started off by considering I may be able to simply have a region specified from within my twig menu loop.
If you want to add something to your template, you need to use preprocess function in your module/theme. For example, you can add view variable using views_embed_view function:
/**
* Implements hook_preprocess_HOOK().
*/
function MYTHEME_preprocess_menu(&$variables) {
switch ($variables['menu_name']) {
case 'mega-menu':
$variables['my_view'] = views_embed_view('my_view');
break;
}
}
After this, $my_view variable will be defined in your menu--mega-menu.html.twig template.
Adding block is a bit more difficult. Please take a look at this answer.

Placing block inside a node (positioning block between specific elements in node's content)

Basically I created a webform and enabled it as a block, now I want to put that block inside a specific node. I can do that by placing it in a 'content' region and defining the specific node BUT it displays at the end of the content. Now how can I move it between specific elements inside the content?
The node is using a page-type....tpl.php which is used by 5 other nodes as well so I cannot change the code.
To visualize it looks like:
[ content ]
-description text-
-list of videos-
[ end of content ]
and I need to put my webform between the text and the video list. Is there a way?
There are many roads you could take, but since you said you're considering the template file: Why not use a node-specific template, since page is a node type?
Say you're on node/123, then you could use a template named node--123.tpl.php
(see Drupal 7 Template (Theme Hook) Suggestions) and embed your block right there.
Alternatively, you could provide a reusable token in a custom module via hook_token_info and combine it with the commonly used token_filter module. But that might be over the top, if it's just one node you need to touch.
For Drupal 7 a bit of a hacky way to display the contents of a block in content would be to enable the PHP Filter module. Then edit your node and switch to the PHP code Text format and add this code
<?php
$block = module_invoke('block', 'block_view', '1');
print render($block['content']);
?>
where '1' is the block id found in the URL when you edit the block and be sure to include the PHP tags.
Also see this page https://www.drupal.org/node/26502 for more information on placing blocks.
You can use the EVA module to add the webforms in the node as a field.
You basically create a view and choose the "eva field" option then you make sure that this view selects only the webforms you want to have and relates it to the node (the EVA module documentation has much better examples than I can provide).
After you have added it as a field you can place it anywhere in the node.
There is also the Block reference module that could help you.

custom page-xxxx.tpl.php doesnt works

I have page named page--news.tpl.php, which i created for my news page. But after i cleared my cache, page still not using, and drupal use the original page.tpl.php. Any ideas how to solve it?
An alternate way of doing it, is through preprocess hook with few lines of code.
Here's how it goes
function <module_name>_preprocess_page(&$variables) {
if (isset($variables['node'])) {
$variables['theme_hook_suggestions'][] = 'page__'.$variables['node']->type;
}
}
Suppose you have a node type as "news" then tpl should look like 'page--news.tpl.php' and above code will handle the rest.

Displaying a Drupal content type field outside of a node

I have created a custom field on a content type in Drupal 7 which I need to display outside of the node, in a separate area on the page.
Similar to how the $title variable works (in which you can place this where you like in the page.tpl.php file) I would like to be able to create another variable called $subtitle which would call the data from the current node and allow me to print out the variable in an area on the page.tpl.php file.
I've seen a view examples seeming to use views and blocks to accomplish this task, but that seems a bit excessive and wondered if there was an easier way.
There is an easier way, you do need to bear in mind though that not every page is a node page, and not every node page will be of the right content type so you should be selective. Just add this to your theme's template.php file:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type == 'my_type') {
$vars['subtitle'] = $node->field_my_field[LANGUAGE_NONE][0]['value'];
}
}
Then in page.tpl.php you should do something like this:
if (isset($subtitle)) :
echo $subtitle;
endif;
Make sure you clear your caches (at admin/config/development/performance) once you've implemented the hook in template.php or Drupal won't pick it up.

Drupal7: Trying to theme a specific page using a preprocess function, but...I get a blank screen instead

I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)

Resources