How to use nodeload in Drupal 7 to print referred node field? - drupal

I am in a node, I created a field using "References" module to relate one content type to another. Now... The 2 content type are "PRACTISE" (a node with title, description ecc...) and "TECHNOLOGY", a node with just logo images. I want to show related logo into node--practise.tpl.php. How can i do this in DP7?

I wouldn't do it directly in the template file, instead you'd be better off implementing hook_preprocess_node in your theme's template.php file to pass the logo(s) in as a variable. The logic is the same either way:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type == 'practise') {
$related_node_nid = $node->field_related_field_name['und'][0]['nid'];
$related_node = node_load($related_node_nid);
$logos = '';
foreach ($related_node->field_logo_field_name['und'] as $img) {
$logos .= theme('image', array('path' => $img['uri'], 'alt' => 'Alt text'));
}
$vars['related_logos'] = $logos;
}
}
Then in your template.php file you will have the variable $logos which will contain the list of logos you built up in the preprocess function. Obviously you can tailer the HTML to suit your needs, and you need to swap in the correct field names for field_related_field_name and field_logo_field_name.

Related

Drupal 7 Views custom view template fields

I've successfully created a custom view template for my Drupal 7 site but am having issues adding attributes to the content which is outputted. I've searched high and low for the answer to this but to no avail.
I have a view called: views-view-fields--homepage-articles.tpl.php
I am printing content like :
$fields['title']->content
This is fine and expected, and outputs:
Title
But I want to add classes to it - how? I'm thinking I need to write a hook, but I cannot find this documented anywhere. At the moment my solution is a string replace:
<?php print str_replace('<a ', '<a class="brand-blue uppercase nodecoration"', $fields['title']->content); ?>
As you can imagine, this is not a satisfactory or long-term solution.
Many thanks!
You should be able to add the classes to the field using template_preprocess_views_view_fields().
Edit: Couldn't do it the way I thought, but you can overwrite the output of the field like so:
function MY_THEME_preprocess_views_view_fields(&$vars) {
$view = $vars['view'];
if ($view->name == 'node_listing') {
foreach ($vars['fields'] as $id => $field) {
if ($id == 'title') {
$field_output = l($view->result[$view->row_index]->node_title, 'node/'. $view->result[$view->row_index]->nid, array('attributes' => array('class' => 'brand-blue uppercase nodecoration')));
$vars['fields'][$id]->content = $field_output;
}
}
}
}
Have you tried using Semantic Views? https://drupal.org/project/semanticviews - that way you can override the classes within the UI instead of template files, may suit your needs better.

Customize drupal search result page

I have created a customized theme called "test", and I am trying to display the search results.
I added the function test_preprocess_search_results() to the template.php (copied the code from drupal page "function template_preprocess_search_results", then copy the search-result.tpl.php from search module to the "test"'s template folder.
function test_preprocess_search_results(&$variables) {
$variables['search_results'] = '';
if (!empty($variables['module'])) {
$variables['module'] = check_plain($variables['module']);
}
foreach ($variables['results'] as $result) {
$variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
}
$variables['pager'] = theme('pager', array('tags' => NULL));
$variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}
I am new in drupal, my concern is how can I make the search result display in some certain div which claimed in my page.tpl.php file? Do something like <?php print render($page['search_result']); ?> in page.tpl.php div ? I am not sure how does the default theme know where to display the search result? Can anyone help thanks
ps: after what I did, and refresh the cache, nothing show up
For search results you should use the search-results.tpl.php (all results list) and search-result.tpl.php (single list item) templates. Copy them from ROOT/modules/search folder into your theme folder and override them as you prefer.

How to add a body class for the current node's taxonomy to a Drupal 7 theme

Does anyone know how or can guide me in the right direction on how to add a body css class for the current node's taxonomy term? i.e. <body class="term-dogs"> where "dogs" is the taxonomy term name. It could also be just the term ID. Either way is fine I just need a solution. This will be for a Drupal 7 zen sub-theme
This answer took longer than I expected to figure out. The hard part was collecting the terms on the node, since All taxonomy functions relating to nodes have been removed or refactored. Ultimately, page 355 of Pro Drupal 7 Development saved the day with a snippet that does the job previously handled by taxonomy_node_get_terms.
Below is the code that worked for me (look for the part that says "MAGIC BEGINS HERE"). Assuming you're creating a sub-theme of Zen, you'll want to move this to your sub-theme's template.php file and rename it to YOURSUBTHEMENAME_preprocess_html:
/**
* Override or insert variables into the html template.
*
* #param $vars
* An array of variables to pass to the theme template.
* #param $hook
* The name of the template being rendered ("html" in this case.)
*/
function zen_preprocess_html(&$vars, $hook) {
// If the user is silly and enables Zen as the theme, add some styles.
if ($GLOBALS['theme'] == 'zen') {
include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
_zen_preprocess_html($vars, $hook);
}
// Classes for body element. Allows advanced theming based on context
// (home page, node of certain type, etc.)
if (!$vars['is_front']) {
// Add unique class for each page.
$path = drupal_get_path_alias($_GET['q']);
// Add unique class for each website section.
list($section, ) = explode('/', $path, 2);
if (arg(0) == 'node') {
if (arg(1) == 'add') {
$section = 'node-add';
}
elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
$section = 'node-' . arg(2);
}
// MAGIC BEGINS HERE
$node = node_load(arg(1));
$results = field_view_field('node', $node, 'field_tags', array('default'));
foreach ($results as $key => $result) {
if (is_numeric($key)) {
$vars['classes_array'][] = strtolower($result['#title']);
}
}
// MAGIC ENDS HERE
}
$vars['classes_array'][] = drupal_html_class('section-' . $section);
}
if (theme_get_setting('zen_wireframes')) {
$vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
}
// Store the menu item since it has some useful information.
$vars['menu_item'] = menu_get_item();
switch ($vars['menu_item']['page_callback']) {
case 'views_page':
// Is this a Views page?
$vars['classes_array'][] = 'page-views';
break;
case 'page_manager_page_execute':
case 'page_manager_node_view':
case 'page_manager_contact_site':
// Is this a Panels page?
$vars['classes_array'][] = 'page-panels';
break;
}
}
I needed to know how to do this and Matt V's solution worked perfectly. I made a couple of additions to his work. I called drupal_html_class which replaces spaces and invalid characters. And I added in the term ID to allow you to target a term even if the name of the term changes.
// MAGIC BEGINS HERE
$node = node_load(arg(1));
$results = field_view_field('node', $node, 'field_tags', array('default'));
foreach ($results as $key => $result) {
if (is_numeric($key)) {
// Call drupal_html_class to make safe for a css class (remove spaces, invalid characters)
$vars['classes_array'][] = "taxonomy-" . strtolower(drupal_html_class( $result['#title']) );
// Add taxonomy ID. This will allow targeting of the taxonomy class even if the title changes
$vars['classes_array'][] = "taxonomy-id-" . $result['#options']['entity']->tid ;
}
}
// MAGIC ENDS HERE
Not sure what you mean with that body tag, but the classes on the node are generated here:
http://api.drupal.org/api/drupal/modules--node--node.module/function/template_preprocess_node/7
You can add more by implementing yourmodule_preprocess_node($vars) and then add whatever you want to $vars['classes_array']

Theming CCK node in Drupal 5

I need to add a few links to the bottom of node (custom type) form in Drupal 5.
I've tried contemplate module, but it actually deals only with custom fields (contemplate template doesn't contain node title, description, taxonomy fields) - while I need to add links below or above actual body field.
How can I make this possible?
Here is the most useful recipe :
from(Theming CCK Input Form | drupal.org)
Example (to use in themename/template.php file):
<?php
function phptemplate_product_node_form($form) {
global $user;
$vars = array('user' => $user, 'form' => $form);
$vars['title'] = drupal_render($form['title']);
$vars['body'] = drupal_render($form['body_filter']);
// etc
return _phptemplate_callback('product_edit', $vars);
}
?>

How do I alter the #weight of the content generated by the upload.module?

On my Drupal powered website, I want to list available downloads at the top of a node page (within a narrow float:right <div>), not at the bottom where they normally appear.
Within my theme, I've overridden the theme_upload_attachments() function to generate a <div> of width 40%, but this is showing up at the bottom of the page.
Within the upload.module file is code that controls where the attachments are listed on the page:
// function upload_nodeapi(), line #284 of upload.module
$node->content['files'] = array(
'#value' => theme('upload_attachments', $node->files),
'#weight' => 50,
);
If I manually hack this #weight to -1, my custom list of attachments shows where I want, floating at the righthand side of the top of the content area.
However, I don't want to manually hack the core file upload.module, as my changes will be lost next time I apply an upgrade (say, for a security patch).
How/Where do I modify the #weight of content['files'] within my theme code?
Or, am I going about this the wrong way?
You'll need a module to do this, not just a theme. A module can implement hook_nodeapi(), which will give it a chance to change the contents of that $node->content array before it's rendered. If your module is named 'upload_tweaker' for example, you'd use the following function:
function upload_tweaker_nodeapi(&$node, $op) {
if ($op == 'view') {
$node->content['files']['#weight'] = -1;
}
}
Each module gets a crack at changing the node during this 'nodeapi' event; if you want to change the stuff that's added by one module, you need to make sure that your module loads after it. This can be done by naming it something like 'zzz', or by changing its "weight" field in the system table of your site's database. Modules can be weighted just like form elements.
api.drupal.org has more information.
For those who uses CCK and want to alter body weight:
CCK module uses pre_render function
/**
* Pre-render callback to adjust weights of non-CCK fields.
*/
function content_alter_extra_weights($elements) {
if (isset($elements['#content_extra_fields'])) {
foreach ($elements['#content_extra_fields'] as $key => $value) {
// Some core 'fields' use a different key in node forms and in 'view'
// render arrays. Check we're not on a form first.
if (!isset($elements['#build_id']) && isset($value['view']) && isset($elements[$value['view']])) {
$elements[$value['view']]['#weight'] = $value['weight'];
}
elseif (isset($elements[$key])) {
$elements[$key]['#weight'] = $value['weight'];
}
}
}
return $elements;
}
So due to this callback you are not able to alter weight using normal behavior.
You should do this:
function YOUR_MODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
// Only for node pages.
if ($op == 'view' && $a4) {
$body_weight = 15 // Any weight.
$node->content['#content_extra_fields']['body']['weight'] = $body_weight; // This value CCK module will use to alter body weight in the callback from above.
}
}

Resources