Drupal: where can I get the $content Array in my theme? - drupal

I need to modify the node content Array before it is rendered into html.
For this reason I cannot use the $content variable in my node template. I'm looking for it in template.php file, but I cannot find it.
thanks

AFAIK, you can not access the unrendered node content array from within a theme, as the theme processing occurs to late in the processing cycle (i.e. the content array will already be rendered as you observed).
The standard way to access and modify the node content array before it gets rendered would be to implement hook_nodeapi() within a custom module, reacting to the 'view' operation. This gets invoked after the content array has been assembled, but before it gets rendered, allowing you to adjust it at will.
Be aware that other modules might do this as well - if that is the case and you want to adjust values provided by other modules, the call order of the modules becomes relevant and you might need to adjust your modules weight to ensure it gets called after the others.

Original function that generates variables available to the node is: http://api.drupal.org/api/function/template_preprocess_node/6
You can modify template variables by implementing your own node preprocess function inside template.php that will execute after original function therefore allowing you to add your own variables:
function phptemplate_preprocess_node(&$vars, $hook) {
// Here you can add your custom variable...
$vars['myContent'] = "something";
}

Related

Override item-list.html.twig & time.html.twig inside a view

I've created a view that outputs a <ul> with a <time> element in each <li>. I need to convert the HTML list to a select.
So I did 2 things:
Copied item-list.html.twig and changed <ul> to a <select>
Copied time.html.twig and changed <time> to <option>
Although this works I need those 2 templates to be scoped to my view named: 'vcon-selection-table'. I tried many things but nothing works:
views-view-table--vcon-selection-table--item-list.html
views-view-table--vcon-selection-table-item-list.html
views-view-table--vcon-selection-table.html--dataset-item-list
So the question is: What template name should I use to override the templates item-list.html.twig and time.html.twig inside my view?
Is the purpose of that view to only provide that <select> element and <option> elements? Then this really isn't the way to go. Don't build a form or form elements with Views.
You'd better provide a custom form or a custom item_list in a custom block. The block then can be placed where ever you need it. There are plenty of tutorials out there. Maybe take the following as a starting point: https://valuebound.com/resources/blog/creating-a-custom-form-in-a-block-in-two-steps-in-Drupal-8
Otherwise, if you really want to continue your road I think you have to simply preprocess the list and time fields to switch to a different template according your desired conditions. I strongly guess there is no option that a custom template for a field from a node in a view will automatically be taken into account by naming patterns. You have to add that template suggestion manually first.
Snippet to be placed in MYMODULE.module file or MYTHEME.theme file.
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function MYMODULE/MYTHEME_theme_suggestions_item_list_alter(array &$suggestions, array $variables) {
// Use \Drupal::routeMatch() to get the current view ID or some other indicator.
// Sample: Get the current view ID.
$view_id = \Drupal::routeMatch()->getRouteObject()->getDefault('view_id');
// Install Devel, Kint module, then uncomment the following line and flush caches to inspect the outcome.
// ksm($view_id);
// Add the template suggestion.
// Add your template now under /templates/item-list--VIEWID.html.twig
if (!empty($view_id)) {
$suggestions[] = 'item_list__' . $view_id;
}
}
Downside of this approach is that this would trigger all item_lists that live on that exact page/view to take this template. You could fine-grain your templating much better with a custom module and a custom item_list and/or form.
Alternatively you can also move the logic to the date field and a custom view mode and an extra template for that. Then at least it stays where it belongs to.
To enable Twig debugging and getting the built-in template suggestions printed as HTML comments follow this article: https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates

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 :)

Change size of user/password login box

I don't know how to change the size of the login username/password boxes on the drupal site that I'm trying to build. I'm stumbling through the theming, and don't know where to find the file that needs to be changed in order to have boxes that fits the aesthetic (so a file path would be very helpful).
I'm hoping it's a css solution. You can see the site first hand at innovatefortomorrow[dot]org and my firebug screenshot http://www.jonrwilson.com/user-login-form.png (I don't have enough reputation points to attach an image or two hyperlinks).
Thanks!
read this as well! This is an alternative answer!
Ok... you are about to enter one of the most exciting and complex features of Drupal: the form API or - for brevity - FAPI. Some theory first, and then the solution! :)
All forms in Drupal are built by the drupal_get_form() function, that accepts an array as parameter. Each field in the array is basically a field of your form, and each field has a number of proprieties, each of them define additional characteristics of the the field, like for example its default value, if it is required or optional and - yes - in the case of textfields... how large they have to be! You can find a detailed explanation of the structure of form arrays here on the drupal site.
The beauty of the form API is that the function that renders the form invokes a number of hooks at various moments during its building process, so you can implement these hooks in order to "alter" a form before it is finalised and sent to the browser.
The most commonly hooks for form alteration are hook_form_alter() and hook_form_FORM_ID_alter(). The first is executed for any form processed by the drupal engine, the latter only for the specific form named "FORM_ID". I will not get into any more details on the internal working of the form API, but here you can read more.
As for your specific case, I assume you are using the standard "user block" shipping with Drupal. In this case I suggest you implement hook_form_FORM_ID_alter() in a form similar to this one:
mymodule_form_user_login_block_alter(&$form, $form_state) {
$form['pass']['#size'] = 43;
}
Hope this helps! :)
I think in this case you have to go to the your html ( or tpl), not your css file to edit it.
One quick way is to search the relevant string (i.e., size="43" name="name" etc) in order to find the correct part.
Here is the way to theme a user login form in drupal 6 with the preprocess function in a template file and not in a module.
in template.php put this code:
function yourThemename_preprocess_user_login(&$variables) {
$variables['form']['name']['#size'] = 15;
$variables['form']['pass']['#size'] = 15;
$variables['rendered'] = drupal_render($variables['form']);
}
create a new file user-login.tpl.php (if it's not already there) and just paste this:
<?php print $rendered; // this variable is defined in the preprocess function user_login and print the login form ?>
Don't forget to clear theme cache or system cache in the performance settings.

How do you modify the fields output in Drupal's RSS Feeds

Trying to modify the RSS feeds created by Views module in Drupal.
Since there are no 'theme_' hooks for the RSS feeds (rightfully as XML is theme-less), I need an alternate way to modify the fields that are output into the RSS, preferably using template.php if possible.
http://api.drupal.org/api/function/format_rss_item/6 looks promising as that is where each row is created, but it doesn't
node_feed() is what collects the nodes, creates the additional fields, and then calls format_rss_item().
Specifically, we need to remove the dc:creator element from the $extra array created in node_feed()
If you go into a specific content type, you can set the RSS display fields as per this post:
http://redfinsolutions.com/redfin-blog/show-hide-fields-views-generated-drupal-rss-feed
I am adding another answer, as I have recently had to do this and managed to do it without modifying data in the theme layer.
You can add a preprocess function to your view. It is a bit of work though.
There are some guidelines here, but they are a bit confusing. Here is my summary of how to do this.
First make sure your modules weight is > views
Secondly copy the template you would like to add a preprocessor to to your modules directory. Rename it to be something in the list of templates in theming information.
Then edit hook theme like this (but change to use the existing view that you need to override.
function mymodule_theme($existing, $type, $theme, $path) {
// Copy the exsisting views theme from the existing array.
$override = $existing['views_view_row_rss'];
// Add my preprocessor to the list of preprocess functions
$override['preprocess functions'][] = 'mymodule_myfunction';
// Point to a template so that this view will be used.
$override['template'] = 'my_more_specific_template_name';
$override['path'] = drupal_get_path('module', 'path');
unset($override['file']);
// Return your theme handler.
return array('my_more_specific_template_name' => $override);
}
You should then be able to write your preprocess code in the function mymodule_myfunction.
In the view, if you click on "style information" this will show you the template files used to create the feed. You can copy the template so that it is overridden for your view and remove dc:creator from the $item_elements array.
This is not particularly nice as you are modifying data in the theme layer, but it will do what you want.
I'd suggest using the Views Node Feed module to do this. It will let you completely write the XML that is output by Views for feeds.

Resources