I'm stuck trying to pass variables from my theme_preprocess_field function to my template file. I want to have a variable for each of the fields I am printing out to my template.

I'm using the Paragraphs module in Drupal 7, so that might be throwing me off, but I feel like I'm really close. Here's a simple example:

Preprocess function in my template.php:

function merge_preprocess_field(&$variables) {
    $element = $variables['element'];
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'field_title') {
      $variables['test_title'] = $variables['items'][0]['#markup'];

The name of the template I am using is paragraphs-item--full-text.tpl.php.

field_title is the name of a field inside a paragraphs bundle called full-text.

Everything that I have read so far indicates that I should be able to access the $variables['test_title'] variable in my template this way:

<?php print render($test_title); ?>

It doesn't print, even after clearing the cache. It's even showing up in the array when I krumo($variables);. Am I missing something?

  • Is your theme called "merge"? If not then your function is not named properly. – Queenvictoria Apr 17 '15 at 22:18
  • Yes the name of my theme is merge. Is it possible that it would only show up in the field--field-title.tpl.php template? – bgrobertson Apr 19 '15 at 22:11

Ok, I figured this out. My preprocess function was for a field, but I was trying to access the variable in the wrong template. What I ended up doing was creating individual preprocess functions for each paragraphs-item, and preprocessing the variables in there.

First, I created a function that would allow me to create preprocess functions for entity types:

function merge_preprocess_entity(&$vars, $hook) {
  $function = 'merge_preprocess_' . $vars['entity_type'];
  if (function_exists($function)) {
    $function($vars, $hook);

This allowed me to create a preprocess function for paragraphs items. Then I used this same idea to create preprocess functions for individual paragraphs items:

function merge_preprocess_paragraphs_item(&$vars, $hook) {
  $elements = $vars['elements'];

  $function = 'merge_preprocess_paragraphs_item__' . $elements['#bundle'];
  if (function_exists($function)) {
    $function($vars, $hook);

Now I can set variables that are available in the paragraphs items templates, like so:

function merge_preprocess_paragraphs_item__smaller_full_width_text(&$vars, $hook) {

  if ($vars['content']['field_cta']) {

    $field_cta = !empty($vars['content']['field_cta'])? $vars['content']['field_cta'][0]['entity']['field_collection_item']: '';

    foreach ($field_cta as $field_cta) {

        // Make the CTA url available to the rest of the block
        $vars['content']['cta_url'] = $field_cta['field_link'][0]['#markup'];

        // Set character limit for CTA Text
        $vars['content']['cta_text'] = truncate_utf8($field_cta['field_link_text'][0]['#markup'], 24, TRUE, FALSE);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.