11

What I have installed on my site is the Entity API and Entityform modules. The Entityform module allows me to create forms that any user can submit and the forms fields are available for me to use in views, similar to how a content type would work.

Normally to pull a field's value from a node into my template with php I'd do the following:

<?php echo $node->field_name[$node->language][0]['value']; ?>

However this does not work with entityforms/entities.

Does anybody know of a way to do this for a field within an entityform? Some people have suggested to use devel and the dpm() function but I have no idea how to use those either.

Some specific examples on how I can get this field's value would really help me a lot.

6 Answers 6

14

Have you tried accessing the values with entity_metadata_wrapper?

$entityform_id = 12; // entity form id
$entityform = entityform_load($entityform_id);
$entityform_wrapper = entity_metadata_wrapper('entityform', $entityform);
// Access fields with $entityform_wrapper->field_name->value() or ->field_name->raw()
drupal_set_message($entityform_wrapper->field_my_field->value()); // for example
3
  • 4
    Note for future readers: This is correct, but to access an entity field is not necessary to install the Entity API module; Drupal core already has the functions to access an entity field.
    – avpaderno
    Commented Jan 20, 2013 at 9:27
  • Yes, but OP tagged and specified they had the Entity API module installed, hence entity_metadata_wrapper, furthermore it is a dependency of the entityforms module. Commented Jan 20, 2013 at 21:18
  • 2
    entity-api is the tag for the Entity API implemented from Drupal 7, not for the Entity API module. The note is to avoid beginner developers think the Entity API module is necessary for handling entities. I didn't say the answer was not correct. :)
    – avpaderno
    Commented Jan 20, 2013 at 21:25
8

Without using the Entity API module, to get the value of a field associated with an entity, you use field_get_items(); you just pass the entity type, the entity object, and the field name.

For example, to get the body of a comment, you use the following code.

if ($items = field_get_items('comment', $comment, 'comment_body')) {
  // ...
}

To get the body of a node, you could use the following code.

if ($items = field_get_items('node', $node, 'body')) {
  // ...
}

The entity object is the object returned from entity_load(), or a specific load function for that entity, such as node_load(), comment_load(), or taxonomy_term_load(). In Drupal 8, those functions are wrappers for entity_load().

If you want to show the content of the field, then you should use field_view_field().

0

You normally can get at $node properties because you or some code you're using is calling node_load() beforehand.

You need to fully load an entity before you can get at it's properties.

Use entity_load() http://api.drupal.org/api/drupal/includes%21common.inc/function/entity_load/7

Then you'll actually have a loaded entity object that you can get at the fields of.

Using dpm() is the same as any other function, you just need to install the devel module.

So, do something like this:

<?php
$entity = entity_load('myEntityType', $entity_id);
dpm($entity);
?>
0
0

Try:

$value= field_get_items('entityform', $entityform, 'field_myfieldname');
print '<p>the value is: '.$value.'</p>';

This should print the value of the field.

If it's an array try this:

$values = field_get_items('entityform', $entityform, 'field_myfieldname');
$value = $values[0]['value'];
print '<p>the value is: '.$value.'</p>';

Remember that this is the raw output and, depending on where the value comes from, you may have to sanitise the value manually here.

See: function field_get_items

0

Place this inside one of your modules and go to the form's page and you will find the info you are looking for.

function yourmodulename_form_alter(&$form, &$form_state, $form_id) {

  dpm($form);

}
-1

Rather than using dpm and installing that module I use:

print_r('<pre>');
print_r($node);
print_r('</pre>');

Hopefully this will help you dig down to the value.

3
  • Hmmm, that's not outputting anything for me :( Could it be because I'm viewing an entityform submission, and not a node? Or would an entityform submission actually be a node? So confused.
    – Dustin
    Commented Oct 11, 2012 at 19:53
  • I'm not sure then, I would assume that you would need a different variable other than $node.
    – Brian
    Commented Oct 12, 2012 at 18:35
  • 1
    The question is about accessing a field of an entity, not how to print the entity.
    – avpaderno
    Commented Jan 20, 2013 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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