Reputation: 143
Creating a sub-theme in Drupal 7's page.tpl.php
and needing to pull the value (plain text) from field_EXAMPLE
from a custom content type outside of where the rest of the content would normal be.
<!-- Adding $title as normal-->
<?php print render($title_prefix); ?>
<?php if (!empty($title)): ?>
<h1><?php print $title; ?></h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
<!-- THE ISSUE: Adding field_EXAMPLE -->
<h2> <?php print render($field_EXAMPLE;);?> </h2>
...
<!-- Where the rest of the content loads by default -->
<div><?php print render($page['content']); ?></div>
Would field_get_items
work?
function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
return isset($entity->{$field_EXAMPLE}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}
Or this?
$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];
Do I put this in page.tpl.php
?
Tried them but no dice. -Novice
Here is var_dump(get_defined_vars());
["field_thestring"]=>
array(1) {
["und"]=>
array(1) {
[0]=>
array(3) {
["value"]=>
string(44) "This is a string of text please refer to me "
["format"]=>
NULL
["safe_value"]=>
string(44) "This is a string of text please refer to me "
}
}
}
Upvotes: 1
Views: 1887
Reputation: 143
Lets assume that you created a field called field_thestring
that you want to render for a content type article
's page at a location outside of THEME
's outside of where page's content renders.
Step 1. Copy the theme's page.tpl.php. and rename it page--article.tpl.php
.
Step 2. In page.var.php
,
function THEME_preprocess_page(&$variables) {
// To activate page--article.tpl.php
if (isset($variables['node']->type)) {
$nodetype = $variables['node']->type;
$variables['theme_hook_suggestions'][] = 'page__' . $nodetype;
}
// Prevent errors on other pages
if ($node = menu_get_object()) {
if ( !empty($node) && $node->type == 'article') {
$fields = field_get_items('node', $node, 'field_thestring');
$index = 0;
$output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
$variables['thestring'] = $output;
}
else{
$variables['thestring'] = 'Angry Russian: How this error?';
}
}
}
Step 3. In page--article.tpl.php
add <?php print render($thestring); ?>
Initially, I wanted to require all content types to have another field since all Content Types has a Title. Determined it wasn't a great idea for further development.
Upvotes: 1
Reputation: 2173
You can use a preprocess to add value into $variables passed to template
https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x
In template.php :
MYTHEMENAME_preprocess_page(&variable){
$values = current(field_get_items('node', $variable['node'],'field_machine_name'));
$variable['myvar'] = $values['value'];
}
In your template.tpl.php
echo $myvar; // contains your value
Upvotes: 0
Reputation: 7114
You can place your code directly into page template, but you can also place it in page preprocess hook, set the template variable and use that variable from your page template:
https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x
It's kinda cleaner way, but both should work.
Also, try deleting Drupal's cache if you don't see your change immediately on front-end.
And for getting node id try:
global $node;
$nid = $node->nid;
$node = node_load($nid);
...
And if that doesn't work try this:
if ($node = menu_get_object()) {
// Get the nid
$nid = $node->nid;
$node = node_load($nid);
...
}
or this way:
if (arg(0) == 'node' && is_numeric(arg(1))) {
// Get the nid
$nid = arg(1);
$node = node_load($nid);
...
}
Upvotes: 0
Reputation: 21
$node = node_load($nid);
$example_value = $node->field_EXAMPLE[$node->language][0]['value'];
<h2> <?php print $example_value;?> </h2>
or,
$node = node_load($nid);
$values = field_get_items('node', $node, 'EXAMPLE');
if ($values != FALSE) {
$val = $values[0]['value'];
}
else {
// no result
}
<h2> <?php print $example_value;?> </h2>
Upvotes: 0