Pull merge.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d7 / hook / field_update_forbid.twig
1 /**
2  * Implements hook_field_update_forbid().
3  */
4 function {{ machine_name }}_field_update_forbid($field, $prior_field, $has_data) {
5   // A 'list' field stores integer keys mapped to display values. If
6   // the new field will have fewer values, and any data exists for the
7   // abandoned keys, the field will have no way to display them. So,
8   // forbid such an update.
9   if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
10     // Identify the keys that will be lost.
11     $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values']));
12     // If any data exist for those keys, forbid the update.
13     $query = new EntityFieldQuery();
14     $found = $query
15       ->fieldCondition($prior_field['field_name'], 'value', $lost_keys)
16       ->range(0, 1)
17       ->execute();
18     if ($found) {
19       throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data");
20     }
21   }
22 }