View source
<?php
module_load_include('inc', 'title', 'title.core');
module_load_include('inc', 'title', 'title.field');
function title_module_implements_alter(&$implementations, $hook) {
if (isset($implementations['title'])) {
$group = $implementations['title'];
unset($implementations['title']);
switch ($hook) {
case 'entity_info_alter':
case 'entity_presave':
case 'field_attach_presave':
$implementations['title'] = $group;
break;
case 'entity_load':
if (isset($implementations['entity_translation'])) {
$length = array_search('entity_translation', array_keys($implementations)) + 1;
$implementations = array_merge(array_slice($implementations, 0, $length, TRUE), array(
'title' => $group,
), array_slice($implementations, $length, count($implementations) - 1, TRUE));
}
break;
default:
$implementations = array(
'title' => $group,
) + $implementations;
}
}
}
function title_entity_info_alter(&$info) {
foreach ($info as $entity_type => $entity_info) {
if (!empty($entity_info['fieldable']) && !empty($info[$entity_type]['field replacement'])) {
foreach ($info[$entity_type]['field replacement'] as $legacy_field => $data) {
$fr_info =& $info[$entity_type]['field replacement'][$legacy_field];
if (empty($fr_info['field']['field_name'])) {
$fr_info['field']['field_name'] = $legacy_field . '_field';
}
$fr_info['instance']['field_name'] = $fr_info['field']['field_name'];
$type = $fr_info['field']['type'];
if (empty($fr_info['callbacks'])) {
$fr_info['callbacks'] = array();
}
$fr_info['callbacks'] += array(
'sync_get' => "title_field_{$type}_sync_get",
'sync_set' => "title_field_{$type}_sync_set",
);
if (isset($entity_info['entity keys']['label']) && $entity_info['entity keys']['label'] == $legacy_field) {
if (isset($info[$entity_type]['label callback'])) {
$info[$entity_type]['label fallback']['title'] = $info[$entity_type]['label callback'];
}
$info[$entity_type]['label callback'] = 'title_entity_label';
$fr_info += array(
'preprocess_key' => $info[$entity_type]['entity keys']['label'],
);
}
}
}
}
}
function title_field_replacement_info($entity_type, $legacy_field = NULL) {
$info = entity_get_info($entity_type);
if (empty($info['field replacement'])) {
return FALSE;
}
if (isset($legacy_field)) {
return isset($info['field replacement'][$legacy_field]) ? $info['field replacement'][$legacy_field] : FALSE;
}
return $info['field replacement'];
}
function title_entity_label($entity, $type, $langcode = NULL) {
$entity_info = entity_get_info($type);
$legacy_field = $entity_info['entity keys']['label'];
$info = $entity_info['field replacement'][$legacy_field];
list(, , $bundle) = entity_extract_ids($type, $entity);
if (title_field_replacement_enabled($type, $bundle, $legacy_field)) {
$langcode = field_language($type, $entity, $info['field']['field_name'], $langcode);
$values = $info['callbacks']['sync_get']($type, $entity, $legacy_field, $info, $langcode);
return isset($values[$legacy_field]) ? $values[$legacy_field] : $entity->{$legacy_field};
}
if (isset($entity_info['label fallback']['title']) && function_exists($entity_info['label fallback']['title'])) {
$label = $entity_info['label fallback']['title']($entity, $type, $langcode);
return isset($label) ? $label : $entity->{$legacy_field};
}
return property_exists($entity, $legacy_field) ? $entity->{$legacy_field} : NULL;
}
function title_entity_presave($entity, $entity_type) {
$entity_langcode = title_entity_language($entity_type, $entity);
$langcode = $entity_langcode;
if (module_invoke('entity_translation', 'enabled', $entity_type)) {
$langcode = title_active_language();
$translations = entity_translation_get_handler($entity_type, $entity)
->getTranslations();
if (!isset($translations->data[$langcode])) {
$langcode = isset($translations->hook[$langcode]['hook']) && $translations->hook[$langcode]['hook'] == 'delete' ? FALSE : $entity_langcode;
}
}
if ($langcode) {
title_entity_sync($entity_type, $entity, $langcode, TRUE);
}
if ($entity_langcode != $langcode) {
list($id, , ) = entity_extract_ids($entity_type, $entity);
$sync =& drupal_static('title_entity_sync', array());
unset($sync[$entity_type][$id]);
title_entity_sync($entity_type, $entity, $entity_langcode);
}
}
function title_field_attach_update($entity_type, $entity) {
$sync =& drupal_static('title_field_attach_presave', array());
list($id, , ) = entity_extract_ids($entity_type, $entity);
unset($sync[$entity_type][$id]);
title_entity_sync($entity_type, $entity);
}
function title_field_attach_load($entity_type, $entities, $age, $options) {
if ($age == FIELD_LOAD_REVISION) {
title_entity_sync_static_reset($entity_type, array_keys($entities));
}
title_entity_load($entities, $entity_type);
}
function title_entity_load($entities, $type) {
if (module_exists('entity_translation')) {
module_invoke('entity_translation', 'entity_load', $entities, $type);
}
foreach ($entities as &$entity) {
$language = NULL;
if (preg_match('/node\\/\\d+\\/edit/', current_path()) == 0 && preg_match('/node\\/\\d+$/', current_path()) == 0) {
if (!empty($entity->language)) {
$language = $entity->language;
}
}
title_entity_sync($type, $entity, $language, !empty($GLOBALS['title_field_replacement_init']));
}
}
function title_entitycache_load($entities, $type) {
title_entity_load($entities, $type);
}
function title_entitycache_reset($ids, $entity_type) {
title_entity_sync_static_reset($entity_type, $ids);
}
function title_entity_prepare_view($entities, $type, $langcode) {
foreach ($entities as &$entity) {
title_entity_sync($type, $entity, $langcode);
}
}
function title_field_replacement_enabled($entity_type, $bundle, $legacy_field) {
$info = title_field_replacement_info($entity_type, $legacy_field);
if (!empty($info['field']['field_name'])) {
$instance = field_info_instance($entity_type, $info['field']['field_name'], $bundle);
}
return !empty($instance);
}
function title_field_replacement_toggle($entity_type, $bundle, $legacy_field) {
$info = title_field_replacement_info($entity_type, $legacy_field);
if (!$info) {
return;
}
$field_name = $info['field']['field_name'];
$instance = field_info_instance($entity_type, $field_name, $bundle);
if (empty($instance)) {
$options = variable_get('title_' . $entity_type, array());
$field = field_info_field($field_name);
if (empty($field)) {
field_create_field($info['field']);
}
$info['instance']['entity_type'] = $entity_type;
$info['instance']['bundle'] = $bundle;
$info['instance']['settings']['hide_label']['page'] = isset($options['hide_label']['page']) ? $options['hide_label']['page'] : FALSE;
$info['instance']['settings']['hide_label']['entity'] = isset($options['hide_label']['entity']) ? $options['hide_label']['entity'] : FALSE;
field_create_instance($info['instance']);
return TRUE;
}
field_delete_instance($instance);
return FALSE;
}
function title_field_replacement_batch_set($entity_type, $bundle, $legacy_field) {
$batch = array(
'title' => t('Replacing field values for %field', array(
'%field' => $legacy_field,
)),
'operations' => array(
array(
'title_field_replacement_batch',
array(
$entity_type,
$bundle,
$legacy_field,
),
),
),
);
batch_set($batch);
}
function title_field_replacement_batch($entity_type, $bundle, $legacy_field, &$context) {
$info = entity_get_info($entity_type);
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $entity_type);
if (!empty($info['efq bundle conditions'])) {
$query
->entityCondition('bundle', $bundle);
}
if (empty($context['sandbox'])) {
$count_query = clone $query;
$total = $count_query
->count()
->execute();
$context['sandbox']['steps'] = 0;
$context['sandbox']['progress'] = 0;
$context['sandbox']['total'] = $total;
}
$step = variable_get('title_field_replacement_batch_size', 5);
$start = $step * $context['sandbox']['steps']++;
$results = $query
->entityCondition('entity_type', $entity_type)
->range($start, $step)
->execute();
if (!empty($results[$entity_type])) {
$ids = array_keys($results[$entity_type]);
title_field_replacement_init($entity_type, $bundle, $legacy_field, $ids);
$context['sandbox']['progress'] += count($ids);
}
if ($context['sandbox']['progress'] != $context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['total'];
}
}
function title_field_replacement_init($entity_type, $bundle, $legacy_field, $ids) {
$GLOBALS['title_field_replacement_init'] = TRUE;
$entities = entity_load($entity_type, $ids);
foreach ($entities as $id => $entity) {
list(, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
if ($entity_bundle == $bundle) {
field_attach_presave($entity_type, $entity);
field_attach_update($entity_type, $entity);
}
}
unset($GLOBALS['title_field_replacement_init']);
}
function title_entity_sync($entity_type, &$entity, $langcode = NULL, $set = FALSE) {
$sync =& drupal_static(__FUNCTION__, array());
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (!isset($langcode)) {
$langcode = $set ? title_entity_language($entity_type, $entity) : title_active_language();
}
if (is_object($langcode)) {
$langcode = $langcode->language;
}
if (!$set && !empty($id) && !empty($sync[$entity_type][$id][$langcode][$set])) {
return;
}
$sync[$entity_type][$id][$langcode][$set] = TRUE;
$fr_info = title_field_replacement_info($entity_type);
if ($fr_info) {
foreach ($fr_info as $legacy_field => $info) {
if (title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
$function = 'title_field_sync_' . ($set ? 'set' : 'get');
$function($entity_type, $entity, $legacy_field, $info, $langcode);
}
}
}
}
function title_entity_sync_static_reset($entity_type, $entity_ids = NULL) {
$sync =& drupal_static('title_entity_sync', array());
if (is_array($entity_ids)) {
foreach ($entity_ids as $id) {
unset($sync[$entity_type][$id]);
}
}
else {
unset($sync[$entity_type]);
}
}
function title_field_sync_get($entity_type, $entity, $legacy_field, $info, $langcode = NULL) {
if (property_exists($entity, $legacy_field)) {
$entity->{$legacy_field . '_original'} = $entity->{$legacy_field};
$langcode = field_language($entity_type, $entity, $info['field']['field_name'], $langcode);
$values = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
foreach ($values as $name => $value) {
if ($value !== NULL) {
$entity->{$name} = $value;
}
}
$entity->{$legacy_field . '_language'} = $langcode;
$cache =& drupal_static('field_language');
list($id, , ) = entity_extract_ids($entity_type, $entity);
unset($cache[$entity_type][$id]);
}
}
function title_field_sync_set($entity_type, $entity, $legacy_field, $info, $langcode) {
if (property_exists($entity, $legacy_field)) {
$field = field_info_field($info['field']['field_name']);
$langcode = field_is_translatable($entity_type, $field) ? $langcode : LANGUAGE_NONE;
$info['callbacks']['sync_set']($entity_type, $entity, $legacy_field, $info, $langcode);
}
}
function title_active_language($langcode = NULL) {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['active_language'] =& drupal_static(__FUNCTION__);
}
$active_langcode =& $drupal_static_fast['active_language'];
if (isset($langcode)) {
$active_langcode = $langcode;
}
if (empty($active_langcode)) {
$active_langcode = $GLOBALS['language_content']->language;
}
return $active_langcode;
}
function title_entity_language($entity_type, $entity) {
if (module_exists('entity_translation') && entity_translation_enabled($entity_type)) {
$handler = entity_translation_get_handler($entity_type, $entity, TRUE);
$langcode = $handler
->getLanguage();
}
else {
$langcode = entity_language($entity_type, $entity);
}
return !empty($langcode) ? $langcode : LANGUAGE_NONE;
}
function title_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$fr_info = title_field_replacement_info($entity_type);
if (!empty($fr_info)) {
foreach ($fr_info as $legacy_field => $info) {
if (isset($form[$legacy_field]) && title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
$replaced = isset($form[$legacy_field]['#field_replacement']) ? $form[$legacy_field]['#field_replacement'] : FALSE;
if (!$replaced && isset($form[$legacy_field]['#access'])) {
$form[$info['field']['field_name']]['#access'] = $form[$legacy_field]['#access'];
}
$form[$info['field']['field_name']]['#attributes']['class'] = array(
'form-item-' . $legacy_field,
);
$form[$legacy_field]['#access'] = FALSE;
$form[$legacy_field]['#field_replacement'] = TRUE;
}
}
}
}
function title_field_attach_submit($entity_type, $entity, $form, &$form_state) {
$fr_info = title_field_replacement_info($entity_type);
if (!empty($fr_info)) {
$values = $form_state['values'];
$values = drupal_array_get_nested_value($values, $form['#parents']);
$langcode = entity_language($entity_type, $entity);
foreach ($fr_info as $legacy_field => $info) {
if (!empty($form[$legacy_field]['#field_replacement'])) {
if (!empty($info['callbacks']['submit'])) {
$info['callbacks']['submit']($entity_type, $entity, $legacy_field, $info, $langcode, $values);
}
drupal_static_reset('field_language');
title_field_sync_get($entity_type, $entity, $legacy_field, $info, $langcode);
}
}
drupal_array_set_nested_value($form_state['values'], $form['#parents'], $values);
}
}
function title_menu() {
$items = array();
foreach (entity_get_info() as $entity_type => $entity_info) {
if (!empty($entity_info['field replacement'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
if (isset($bundle_info['admin'])) {
$path = $bundle_info['admin']['path'];
if (isset($bundle_info['admin']['bundle argument'])) {
$bundle_arg = $bundle_info['admin']['bundle argument'];
}
else {
$bundle_arg = $bundle_name;
}
$access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array(
'access callback',
'access arguments',
)));
$access += array(
'access callback' => 'user_access',
'access arguments' => array(
'administer site configuration',
),
);
$path = "{$path}/fields/replace/%";
$field_arg = substr_count($path, '/');
$items[$path] = array(
'load arguments' => array(),
'title' => 'Replace fields',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'title_field_replacement_form',
$entity_type,
$bundle_arg,
$field_arg,
),
'file' => 'title.admin.inc',
) + $access;
}
}
}
}
$items['admin/config/content/title'] = array(
'title' => 'Title settings',
'description' => 'Settings for the Title module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'title_admin_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'title.admin.inc',
);
return $items;
}
function title_help($path, $arg) {
switch ($path) {
case 'admin/config/content/title':
return '<p>' . t('The settings below allow to configure the <em>default</em> settings to be used when creating new replacing fields. It is even possible to configure them so that the selected fields are created automatically when a new bundle is created.') . '</p>';
}
}
function title_field_extra_fields_alter(&$info) {
$entity_info = entity_get_info();
foreach ($info as $entity_type => $bundles) {
foreach ($bundles as $bundle_name => $bundle) {
if (!empty($entity_info[$entity_type]['field replacement'])) {
foreach ($entity_info[$entity_type]['field replacement'] as $field_name => $field_replacement_info) {
if (title_field_replacement_enabled($entity_type, $bundle_name, $field_name)) {
unset($info[$entity_type][$bundle_name]['form'][$field_name], $info[$entity_type][$bundle_name]['display'][$field_name]);
}
}
}
}
}
}
function title_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
module_load_include('inc', 'title', 'title.admin');
title_form_field_ui_overview($form, $form_state);
}
function title_tokens_alter(array &$replacements, array $context) {
$mapping =& drupal_static(__FUNCTION__);
if (empty($mapping)) {
foreach (entity_get_info() as $entity_type => $info) {
if (!empty($info['token type'])) {
$mapping[$info['token type']] = $entity_type;
}
}
}
if (isset($mapping[$context['type']])) {
$entity_type = $mapping[$context['type']];
$fr_info = title_field_replacement_info($entity_type);
if ($fr_info && !empty($context['data'][$context['type']])) {
$entity = $context['data'][$context['type']];
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$options = $context['options'];
$langcode = isset($options['language']) ? $options['language']->language : entity_language($entity_type, $entity);
if ($fr_info) {
foreach ($fr_info as $legacy_field => $info) {
if (isset($context['tokens'][$legacy_field]) && title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
$langcode = field_language($entity_type, $entity, $info['field']['field_name'], $langcode);
$item = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
if (!empty($item)) {
list($value, $format) = array_values($item);
if (empty($format)) {
$replacements[$context['tokens'][$legacy_field]] = check_plain($value);
}
else {
$replacements[$context['tokens'][$legacy_field]] = check_markup($value, $format, $langcode);
}
}
}
}
}
}
}
}
function title_form_field_ui_field_edit_form_alter(&$form, $form_state) {
$instance = $form['#instance'];
$entity_type = $instance['entity_type'];
if (title_field_replacement_is_label($entity_type, $instance['field_name'])) {
$info = entity_get_info($entity_type);
$form['instance']['settings']['hide_label'] = _title_hide_label_widget($instance['settings'], $info['label']);
}
}
function _title_hide_label_widget($default, $entity_label) {
return array(
'#type' => 'checkboxes',
'#title' => t('Label replacement'),
'#description' => t('Check these options if you wish to hide the main page title or each label when displaying multiple items of type %entity_label.', array(
'%entity_label' => $entity_label,
)),
'#default_value' => !empty($default['hide_label']) ? $default['hide_label'] : array(),
'#options' => array(
'page' => t('Hide page title'),
'entity' => t('Hide label in %entity_label listings', array(
'%entity_label' => drupal_strtolower($entity_label),
)),
),
);
}
function title_field_replacement_is_label($entity_type, $field_name) {
$label = FALSE;
$legacy_field = title_field_replacement_get_legacy_field($entity_type, $field_name);
if ($legacy_field) {
$info = entity_get_info($entity_type);
$label = $legacy_field == $info['entity keys']['label'];
}
return $label;
}
function title_field_replacement_get_legacy_field($entity_type, $field_name) {
$result = FALSE;
$fr_info = title_field_replacement_info($entity_type);
if ($fr_info) {
foreach ($fr_info as $legacy_field => $info) {
if ($info['field']['field_name'] == $field_name) {
$result = $legacy_field;
break;
}
}
}
return $result;
}
function title_field_replacement_get_label_field($entity_type, $bundle) {
$instance = FALSE;
$info = entity_get_info($entity_type);
if (!empty($info['field replacement'])) {
$fr_info = $info['field replacement'];
$legacy_field = $info['entity keys']['label'];
if (!empty($fr_info[$legacy_field]['field'])) {
$instance = field_info_instance($entity_type, $fr_info[$legacy_field]['field']['field_name'], $bundle);
}
}
return $instance;
}
function title_field_replacement_hide_label($entity_type, $entity, &$variables, $page = FALSE) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instance = title_field_replacement_get_label_field($entity_type, $bundle);
$settings_key = $page ? 'page' : 'entity';
if (!empty($instance['settings']['hide_label'][$settings_key])) {
if ($page) {
$key = 'title';
}
else {
$info = entity_get_info($entity_type);
$key = $info['field replacement'][$info['entity keys']['label']]['preprocess_key'];
}
$variables[$key] = FALSE;
}
}
function title_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'title') . '/views',
);
}
function title_field_attach_create_bundle($entity_type, $bundle) {
$entity_info = entity_get_info($entity_type);
if (empty($entity_info['field replacement'])) {
return;
}
$options = variable_get('title_' . $entity_type, array());
foreach (array_keys($entity_info['field replacement']) as $legacy_field) {
if (empty($options['auto_attach'][$legacy_field])) {
continue;
}
$field_name = $entity_info['field replacement'][$legacy_field]['field']['field_name'];
if (field_info_instance($entity_type, $field_name, $bundle)) {
continue;
}
title_field_replacement_toggle($entity_type, $bundle, $legacy_field);
$instance = field_info_instance($entity_type, $field_name, $bundle);
if ($instance) {
$params = array(
'@entity_label' => drupal_strtolower($entity_info['label']),
'%field_name' => $instance['label'],
);
drupal_set_message(t('The @entity_label %field_name field was automatically replaced.', $params));
}
}
}
function title_field_info_alter(&$info) {
$supported_types = array(
'taxonomy_term_reference' => TRUE,
);
foreach ($info as $field_type => &$field_type_info) {
if (isset($supported_types[$field_type])) {
if (!isset($field_type_info['settings'])) {
$field_type_info['settings'] = array();
}
$field_type_info['settings'] += array(
'options_list_callback' => 'title_taxonomy_allowed_values',
);
}
}
}
function title_taxonomy_allowed_values($field) {
$theme_registry_cache = drupal_static('theme_get_registry');
$theme_registry_is_rebuilding = is_array($theme_registry_cache) && empty($theme_registry_cache);
$bundle = !empty($field['settings']['allowed_values'][0]['vocabulary']) ? $field['settings']['allowed_values'][0]['vocabulary'] : NULL;
if ($bundle && ($label = title_field_replacement_get_label_field('taxonomy_term', $bundle)) && !$theme_registry_is_rebuilding) {
$options = array();
foreach ($field['settings']['allowed_values'] as $tree) {
$vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary']);
if ($vocabulary && ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'], NULL, TRUE))) {
foreach ($terms as $term) {
$options[$term->tid] = str_repeat('-', $term->depth) . entity_label('taxonomy_term', $term);
}
}
}
return $options;
}
return taxonomy_allowed_values($field);
}
function title_ctools_context_converter_alter($context, $converter, &$value, &$converter_options) {
if (isset($context->type[0]) && 0 === strpos($context->type[0], 'entity:') && empty($converter_options['language'])) {
$plugin = ctools_get_context($context->plugin);
if ($function = ctools_plugin_get_function($plugin, 'convert')) {
$converter_options['language'] = $GLOBALS[LANGUAGE_TYPE_CONTENT];
$value = $function($context, $converter, $converter_options);
}
}
}