Skip to content

Instantly share code, notes, and snippets.

@andreav
Last active December 11, 2015 21:38
Show Gist options
  • Save andreav/4663623 to your computer and use it in GitHub Desktop.
Save andreav/4663623 to your computer and use it in GitHub Desktop.
My drupal 7 snippets
# refs - http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
# refs - http://drupal.stackexchange.com/questions/25148/views-and-contextual-filters-with-entity-reference-field
========================================================
$output = field_view_field('node', $node, 'field_name');
========================================================
That will return the sanitised, rendered field in its entirety, in the current language and in render-array form, so you'll get all the associated markup.
It will use the sites language fallback logic if the field has no values available for the current language. You can also pass an array of formatter options or specify a view mode as an optional argument.
===================================================================
$node = node_load($nid);
$field = field_get_items('node', $node, 'field_name');
$output = field_view_value('node', $node, 'field_name', $field[0]);
===================================================================
# Here I declare my functions or template files
#==============================================
function hook_theme() {
return array(
'exmaple_function' => array(
'variables' => array( 'firstvar' => NULL, 'secondvar' => NULL),
),
'example_template' => array(
'variables' => array( 'firstvar' => NULL, 'secondvar' => NULL),
'template' => 'unfile-SENZA-tpl.php',
),
);
}
# Invoke a theme function
#========================
# here I invoke the func (instead of returning html directly)
function menu_html_callback() {
$firstvar = 'sometext'
return theme( 'example_function', array( $firstvar, 'this is second one') );
}
#================================================================================
# here I define the theme function => anyoune can OVERRIDE this func from now on!
#================================================================================
function example_function( $vars ) {
$output = '<h1>' . $vars['firstvar'] . '</h1>';
return $output;
}
# Invoke a template function
# this uses a template mechanism => no func, but <?php ?> instead
#==================================================================
function menu_renderable_page_callback() {
$first = 'sometext'
$output = array(
'#theme' => 'example_template',
'type' => 'markup',
'firstvar' => $first,
'secondvar' => 'some text',
);
return $output;
}
#================================================================================
# Create a file example_template.tpl.php
#================================================================================
<?php
/*
* This is a comment!
*/
<h1 class="classtxt">This is first variable: <?php print $firstvar ?> </h1>
1. From theme developer click on the item
2. suppose you have:
function called: theme_link
3. Override it by defining:
function mytheme_preprocess_link(&$variables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment