function entity_bundle_plugin_rebuild_fields in Entity bundle plugin 7
Rebuild the fields for a given entity type.
Scan field declarations on all entities to make sure that all declarations of the same field are identical, and create fields and instance in case of success.
1 call to entity_bundle_plugin_rebuild_fields()
- _entity_bundle_plugin_rebuild_all in ./
entity_bundle_plugin.module - Helper function: rebuild the fields for entity types using bundle plugins.
File
- ./
entity_bundle_plugin.module, line 41 - EntityBundlePlugin module.
Code
function entity_bundle_plugin_rebuild_fields($entity_type, $entity_info) {
// Load the bundle plugins.
ctools_include('plugins');
$plugins = ctools_get_plugins($entity_info['module'], $entity_info['bundle plugin']['plugin type']);
$plugins = array_filter($plugins, function ($plugin) {
$r = new ReflectionClass($plugin['class']);
$ret = !$r
->implementsInterface('EntityBundlePluginValidableInterface') || call_user_func(array(
$plugin['class'],
'isValid',
));
return $ret;
});
uasort($plugins, 'ctools_plugin_sort');
// Process the bundle plugins and extract the fields they define.
$fields = array();
foreach ($plugins as $plugin_name => $plugin_info) {
$class = ctools_plugin_load_class($entity_info['module'], $entity_info['bundle plugin']['plugin type'], $plugin_name, 'class');
// Cannot use instanceof: it needs a class instance, not a class name.
if (in_array('EntityBundlePluginProvideFieldsInterface', class_implements($class))) {
$fields[$plugin_name] = call_user_func(array(
$class,
'fields',
));
}
}
// Check if the fields definitions are compatible and gather them.
$field_definitions = array();
foreach ($fields as $plugin_name => $plugin_fields) {
foreach ($plugin_fields as $field_name => $field) {
$field_definitions[$field_name][$plugin_name] = $field['field'];
}
}
unset($plugin_name, $plugin_fields);
foreach ($field_definitions as $field_name => $definitions) {
_entity_bundle_plugin_check_incompatible_fields($field_name, $definitions);
}
unset($field_name, $definitions);
// Now, create or update fields.
foreach ($field_definitions as $field_name => $plugin_fields) {
$field = reset($plugin_fields);
$field += array(
'field_name' => $field_name,
'locked' => TRUE,
);
if ($existing_field = field_info_field($field_name)) {
// Merge default values from the cached field definition with the
// definition that comes from EBP.
$field = _entity_bundle_plugin_array_merge_deep_array(array(
$existing_field,
$field,
));
// Recursively cast to string for a sane comparison.
array_walk_recursive($existing_field, function (&$value) {
$value = $value !== FALSE ? (string) $value : '0';
});
array_walk_recursive($field, function (&$value) {
$value = $value !== FALSE ? (string) $value : '0';
});
if (serialize($existing_field) != serialize($field)) {
field_update_field($field);
}
}
else {
// The field does not exist, create it.
field_create_field($field);
}
}
// Finally, create or update instances.
foreach ($fields as $plugin_name => $plugin_fields) {
foreach ($plugin_fields as $field_name => $field) {
$instance = $field['instance'];
$instance += array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $plugin_name,
);
if ($existing_instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'])) {
// Merge default values from the cached instance definition with the
// definition that comes from EBP.
$instance = _entity_bundle_plugin_array_merge_deep_array(array(
$existing_instance,
$instance,
));
// Recursively cast to string for a sane comparison.
array_walk_recursive($existing_instance, function (&$value) {
$value = $value !== FALSE ? (string) $value : '0';
});
array_walk_recursive($instance, function (&$value) {
$value = $value !== FALSE ? (string) $value : '0';
});
if (serialize($existing_instance) != serialize($instance)) {
field_update_instance($instance);
}
}
else {
field_create_instance($instance);
}
}
}
}