drupaldrupal-7drupal-entities

Use of entity_extract_ids($entity_type, $entity)


I am trying to use entity_extract_ids($entity_type, $entity) where:

$entity_type = The entity type; e.g. 'node' or 'user'.

$entity = The entity from which to extract values.

I have never used this function and don't understand what the second parameter (i.e. $entity) is supposed to be.

I would love to see an example code with this function being used. Thank you.


Solution

  • This function return array($id, $vid, $bundle);

    Example:

    // Define $entity_type.
    // Could be 'node', 'user', 'file', 'taxonomy_term' etc.
    $entity_type = 'node';
    
    // Get $entity object, in our case $node with nid = 1
    $entity = node_load(1);
    
    // print_r will give us something like:
    // Array
    // (
    //     [0] => 1
    //     [1] => 4
    //     [2] => page
    // )
    // Where [0] is nid, [1] is vid and [2] is bundle name of a node with nid = 1
    print_r(entity_extract_ids($entity_type, $entity));
    

    It's better to use function like that:

    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);