Bring Field API Memory improvements from Drupal 7.22 into Message module.

Created on 4 April 2013, about 11 years ago
Updated 15 January 2024, 5 months ago

There's some new field api caching done with Drupal 7.22. In order to fully benefit from it, contrib modules need to update their code a little.

Here's the issue:
[#1915646]

Here's the types of code we need to update:

Iterate on all fields of an entity or a bundle
A. The typical code pattern for this in earlier Drupal 7 releases was :

// Get all field definitions in advance to save individual function calls to field_info_field().
$fields = field_info_fields();
foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  $field = $fields[$field_name];
  // Do something with the $field and $instance
}

B. The memory-efficient way after Drupal 7.22 :

foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
  $field = field_info_field($field_name);
  // Do something with the $field and $instance
}

Iterate on all instances of a text field across all entity types and bundles
A. The typical code pattern for this in earlier Drupal 7 releases was :

foreach (field_info_instances() as $field_name => $instance) {
  $field = $fields[$field_name];
  if ($field['type'] == 'text') {
    // Do something with the $field and $instance
  }
}

B. The memory-efficient way after Drupal 7.22 :

foreach (field_info_field_map() as $field_name => $data) {
  if ($data['type'] == 'text') {
    $field = $fields[$field_name];
    foreach ($data['bundles'] as $entity_type => $bundles) {
      foreach ($bundles as $bundle) {
        $instance = field_info_instance($entity_type, $field_name, $bundle);
        // Do something with the $field and $instance
      }
    }
  }
}

NOTE: field_info_field_map() was added in Drupal 7.22, so contributed D7 modules should only use it within a check on the VERSION constant. Need to test to see if you can use it via

if (VERSION >= '7.22') {
  // snippet B.
}
else {
  // snippet A.
}

There's some places in message which might be able to benefit from a re-factor. I'll let the experts decided, but posting this issue to bring up the potential improvements.

πŸ“Œ Task
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada j0rd

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024