4

I'm trying to get the content type from a node entity.

The entity is being loaded as follows:

$synclist = entity_load_single('entity_synclist', $qid);

Now, how would I then get the content type? Would it be as simple as:

$contenttype = $synclist->type;

Or do I have to do some other Drupal magic?

4

You're not loading a node there, you're loading an entity of type entity_synclist. Its bundle key might be type, but it depends on what's defined in the module's implementation of hook_entity_info().

To be sure, you can get that information from the entity definition and use it to get the bundle like so:

$info = entity_get_info('entity_synclist');
$bundle_key = $info['entity keys']['bundle'];

$synclist = entity_load_single('entity_synclist', $qid);
$bundle = $synclist->$bundle_key;
| improve this answer | |
  • 1
    There's no need to put a single variable in {} just $synclist->$bundle_key; is enough. – user49 Jan 21 '14 at 22:22
  • @chx Yeah, I've been in the Python world recently and they have an "explicit is better than implicit" philosophy - guess it's rubbed off on my PHP too :) – Clive Jan 22 '14 at 17:42
0

Someone suggested here to use the bundle key from the entity into - this is just overdose for me.

Someone suggested to use devel - but this will display the node type only when you via the UI and not progrematiclly.

When using a node, or any other entity, when you know that the type the key you can use it as you suggested - just use the $node->type. Another thing you can use is the entity metadata wrapper which is a helpful tool - you can access field and properties the same way but it's also contain some good function:

$wrapper->getBundle() in order to get the type of the entity($node->type).

$wrapper->label() in order to get the title of the entity($node->title, $user->name).

$wrapper->getIdentifier() in order to get the ID of the entity($node->nid, $user->uid) and many other functions.

| improve this answer | |
0

If you are using the Devel module, you could do a dpm($synclist) in the code.

This will display the all the properties of the node entity. I think you are on the right track.

We just need to verify the type property is referred to as that.

| improve this answer | |

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.