Martin Dean
Martin Dean

Reputation: 43

Access data from Drupal 7 Entities

I have created a number of entities in Drupal 7.

One example is the entity 'structuralunit' , that has multiple bundles that include 'csu_company'.

Given that the structuralunit_id of a given csu_company object is '3' how can I access its name in a function ?

Ideally an answer would look like this:

$id = '3';

get_company_name($id) {

//stuff

return $name
};

Supporting information:

The query:

$id = array(3); $entity = entity_load('structuralunit', $id, array());

The code above, used in devel shows me an array that contains the value I need, but I am not sure if I should be loading the entity in this way, or how to extract data from it to use (I am also unsure, why I need to load an array of one (singular) item as a parameter?).

Final thoughts:

I ultimately have more to do with the value returned - look up how many entities of a different type (task), have an entity reference field set to the value (of the company name), and for each of those task entities extract 'x' fields and place them in an array to loop over.

It is my hope that by understanding the way to load the entity, find, and then extract the data I can wrap my head around the followup tasks.

Cheers!

Upvotes: 0

Views: 272

Answers (1)

Tom Pace
Tom Pace

Reputation: 2387

One important note: entity_load's function definition has an array as the second argument.

entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset
= FALSE)

If you are seeking a single value (as your example shows), you still need to pass it as an array.

You should find the name as a string available:

$entity[<OBJECTID>]->name

Upvotes: 1

Related Questions