It s very hard to understand the attaching of fields to a node or entity ?
The transform of data make me feel dizzy and tired.

Can some one help us show us a few code or vivid explaination about it.
Or recommend some links for us.

Thanks and Regards. Great appreciation.

Comments

VM’s picture

Jaypan’s picture

field_attach_load()

Loads all fields for each entity object in a group of a single entity type. The loaded field values are added directly to the entity objects.

field_attach_load() is automatically called by the default entity controller class, and thus, in most cases, doesn't need to be explicitly called by the entity type module.

qqboy’s picture

I watched this function many times, but still have some puzzles,

function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $options = array()) {
    //  
    //  
}

There is a snippet

  if ($cache_read) {
    // Build the list of cache entries to retrieve.
    $cids = array();
    foreach ($entities as $id => $entity) {
      $cids[] = "field:$entity_type:$id";
    }
    $cache = cache_get_multiple($cids, 'cache_field');
    // Put the cached field values back into the entities and remove them from
    // the list of entities to query.
    foreach ($entities as $id => $entity) {
      $cid = "field:$entity_type:$id";
      if (isset($cache[$cid])) {
        unset($queried_entities[$id]);
        foreach ($cache[$cid]->data as $field_name => $values) {
          $entity->$field_name = $values;
        }
      }
    }
  }

Why unset ($queried_entities[$id]);
and Why $entity->$field_name = $values; again ?

Can some one help us make it clear Thanks.
It s very clear that its a small part of this function. Then realized how difficult drupal code is.
God bless me.

Thanks and Regards.

Jaypan’s picture

Why unset ($queried_entities[$id]);
and Why $entity->$field_name = $values; again ?

To be honest, I have absolutely no idea, because I've never needed to be able to understand that in order to use this function. I've never actually looked at the code in the function itself, because you don't need to understand what is happening within the function, to be able to use the function.

qqboy’s picture