0

I have a couple of simple computed fields that combine other fields (such as first name and last name) but I'm not getting the right values from the fields. I sometimes get only the first letter of a text field (maybe just on the Title field), and when I call a longtext field I'm getting all the HTML markup when I really only want the plain text version of the field.

Here's an example where I'm trying to combine a pile of fields for purposes of searching them from one box:

$omnisearch = $entity->title[LANGUAGE_NONE][0]['value']."
".$entity->body[LANGUAGE_NONE][0]['value']."
".$entity->field_prerequisites[LANGUAGE_NONE][0]['value']."
".$entity->field_keywords[LANGUAGE_NONE][0]['value']."
".$entity->field_teachers[LANGUAGE_NONE][0]['value']."
".$entity->field_organizing_center[LANGUAGE_NONE][0]['value']."
".$entity->field_location[LANGUAGE_NONE][0]['value'];
$entity_field[0]['value'] = $omnisearch;

So is there a better parameter than 'value' to pull the plain/sanitized text from the field? And why am I only getting the first character of the title? I only theoretically grok arrays, so the answer may be obvious to others. Thanks for your help!

1

I'd suggest you to start by understanding the structure of your entity. You could do something like <pre><?php print_r($entity); ?></pre> to print on screen your entity object and understand how to reference each one of its fields.

You'll discover that you could get your title just by using $entity->title (in your example you are getting the first letter because you are treating it as an array). You could also find that not all your fields might be have by the 'value' key.

Once you do that, you could get the fields values using field_get_items(). A basic example:

$items = field_get_items('node', $entity, 'field_keywords', $entity->language);
$item = array_shift($items);
$keywords = $item['value'];

And maybe you could take it one step further and wrap that in a reusable function:

/**
 * Get a node field's first value.
 */
function YOUR_MODULE_node_get_field_single_value($node, $field_name, $key) {
  $items = field_get_items('node', $node, $field_name, $node->language);

  if (!empty($items)) {
    $item = array_shift($items);
    return isset($item[$key]) ? $item[$key] : NULL;
  }
  else {
    return NULL;
  }
}

And call it like this (assuming that this field has a single value in it):

$keywords = YOUR_MODULE_node_get_field_single_value($entity, 'field_keywords', 'value');

Please note that the above function is just an example, your field might have multiple values in it. In that case, you'll need to iterate the array returned by field_get_items.

  • Thanks! I don't have access to the site files (eg: theme or modules) so I can't make a function or easily print_r. I'm trying to get all this done via the Computed Field module which takes a PHP argument to calculate the value to output. I'll see if field_get_items will work here. – R. Durham Jul 24 '15 at 20:22
  • Yeah, basically any PHP code will work in the "Computed code PHP" textarea available in any computed field. So you should be fine. Let me know if you run into any issues. PD. Please note it's not a good practice to include PHP code in a field, but if it's your only option, go ahead. – typologist Jul 24 '15 at 20:32
  • In the computed field I've tried: Computed Code (PHP): '$entity_field[0]['value'] = print_r ($entity);' Display Code (PHP): '$display_output = $entity_field_item['value'];' which results is "1". And I also tried creating a block with PHP input with '<?php print_r($entity); ?>' which throws the error: 'Notice: Undefined variable: entity in eval() (line 2 of /home/public/modules/php/php.module(80) : eval()'d code).' I think there is other stuff going on here which I can't control, as I'm not the owner of the site. – R. Durham Jul 27 '15 at 20:17
  • Well dang. I just discovered a new kind of View filter that I'd never seen before. This actually solves my problem without the use of the computed field at all: drupal.stackexchange.com/questions/73731/… Thanks for your help, @Typologist! – R. Durham Jul 27 '15 at 21:22
  • Awesome, I'm glad you found a solution :) – typologist Jul 27 '15 at 22:21

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.