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.

13

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
| improve this answer | |
  • 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. – kiamlaluno Jan 20 '13 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. – David Thomas Jan 20 '13 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. :) – kiamlaluno Jan 20 '13 at 21:25
7

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().

| improve this answer | |
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);
?>
| improve this answer | |
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

| improve this answer | |
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);

}
| improve this answer | |
-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.

| improve this answer | |
  • 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 Oct 11 '12 at 19:53
  • I'm not sure then, I would assume that you would need a different variable other than $node. – Brian Oct 12 '12 at 18:35
  • 1
    The question is about accessing a field of an entity, not how to print the entity. – kiamlaluno Jan 20 '13 at 9:12

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.