function media_wysiwyg_upgrade_entity_tokens in D7 Media 7.3
Same name and namespace in other branches
- 7.4 modules/media_wysiwyg/includes/media_wysiwyg.upgrade.inc \media_wysiwyg_upgrade_entity_tokens()
Upgrade media tokens in filtered text fields for a given entity.
Only upgraded text fields will be updated in storage, omitting the costly entity_save(). I.e. no new entity revisions.
Parameters
string $entity_type: The entity type.
int $id: The entity ID to scan for media tokens in.
Return value
array The number of found and upgraded tokens in entity, keyed by 'found' and 'upgraded'.
1 call to media_wysiwyg_upgrade_entity_tokens()
- media_wysiwyg_upgrade_content_tokens_batch in modules/
media_wysiwyg/ includes/ media_wysiwyg.pages.inc - Batch API callback.
File
- modules/
media_wysiwyg/ includes/ media_wysiwyg.pages.inc, line 270 - Common pages for the Media WYSIWYG module.
Code
function media_wysiwyg_upgrade_entity_tokens($entity_type, $id) {
$report = array(
'found' => 0,
'upgraded' => 0,
);
// Assert that the entity type has a valid controller class to avoid getting
// php parse errors during token upgrades. See issue #2950150.
$type_info = entity_get_info($entity_type);
if (empty($type_info['controller class'])) {
return $report;
}
if (!($entity = entity_load($entity_type, array(
$id,
)))) {
return $report;
}
$entity = reset($entity);
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
// Map between storage engines and the fields for it that will be updated.
$storages = array();
foreach (media_wysiwyg_filter_fields_with_text_filtering($entity_type, $entity) as $field_name) {
$langcode = field_language($entity_type, $entity, $field_name);
if (!isset($entity->{$field_name}[$langcode])) {
continue;
}
$field = field_info_instance($entity_type, $field_name, $bundle);
$field = field_info_field_by_id($field['field_id']);
$field_id = $field['id'];
foreach ($entity->{$field_name}[$langcode] as &$field_item) {
if (empty($field_item['value'])) {
continue;
}
preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $field_item['value'], $matches);
foreach ($matches[0] as $tag_orig) {
$tag_orig = str_replace(array(
'[[',
']]',
), '', $tag_orig);
$tag_info = drupal_json_decode($tag_orig);
if (!isset($tag_info['type']) || $tag_info['type'] != 'media') {
continue;
}
$report['found']++;
// Perform the actual upgrade.
media_wysiwyg_upgrade_token($tag_info);
$tag_new = drupal_json_encode($tag_info);
if (strcmp($tag_orig, $tag_new) == 0) {
// No changes. Don't bother saving this.
continue;
}
$report['upgraded']++;
$tag_orig = '[[' . $tag_orig . ']]';
$tag_new = '[[' . $tag_new . ']]';
// The field_item is available by reference, so it will updated in the
// entity directly. If several identical tokens exists within this text
// value they will all be replaced here, and the next match iteration
// will not perform any replacement as it the search string will not be
// found. No big deal and no need to add a special case for this.
$field_item['value'] = str_replace($tag_orig, $tag_new, $field_item['value']);
$storages[$field['storage']['type']][$field_id] = $field_id;
}
}
}
// Write updated tokens to storage.
foreach ($storages as $storage => $fields) {
$storage_info = field_info_storage_types($storage);
module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_UPDATE, $fields);
}
return $report;
}