uppdated modules

This commit is contained in:
Bachir Soussi Chiadmi
2017-01-22 16:19:36 +01:00
parent 03be8f82aa
commit 8416e3eea1
748 changed files with 32317 additions and 20900 deletions
@@ -3,6 +3,46 @@ Title 7.x-1.x, xxxx-xx-xx
-------------------------
Title 7.x-1.0-alpha9, 2017-01-13
--------------------------------
#2757739 by dewalt, Pol: Token value is not sanitized, when replaced from title field.
#2813673 by plach, czigor, Stevel: Tests broken since new permission in drupal core.
Title 7.x-1.0-alpha8, 2016-03-28
--------------------------------
#2465141 by DuaelFr, Matthijs, jfrederick: Used entity_uri() options if given.
#2602568 by sdstyles: Fixed title_entity_label() should be documented as
callback implementation.
#2605040 by ShaxA, joelpittet, sylus: Removed recursive call to
entity_get_info().
#2040055 by flux423, cs_shadow, visabhishek, plach, OlyN: Fixed Notice:
Undefined index: safe_value in title_field_formatter_view().
#2426105 by Peacog, jcisio: Fixed Views "Link this field to the original entity"
doesn't work when using relationship.
#2286147 by plach: Language fallback does not work when an entity translation is
unpublished.
#2286145 by plach: Prevent empty translations from being synced into the legacy
field.
#1772116 by duellj, GaëlG | f4o: Fixed Menu link title is not getting node title
by default.
#1779268 by ndobromirov | brycesenz: Undefined index: field_name in
title_field_views_data_alter().
#1441224 by MiroslavBanov | OPIN: Fieldgroup and Title break Manage Fields UI.
#1980520 by StoraH, pbz1912: Fixed Empty wrapper tags.
#1920096 by Johnny vd Laar | GiorgosK: Fixed Title incompatibility with the Term
reference widget
#1991712 by milesw: Fixed Title displays wrong revision using Revisioning
module.
#1907078 by sylus: Fixed Undefined index: field_name() in
title_field_replacement_enabled().
#1961810 by peximo | fcortez: Fixed Wrap tag classes are not being inserted.
#1850866 by B-Prod: Fixed Undefined index 'format' in
title_field_text_with_summary_sync_set().
#1269076 by plach, das-peter, danielnolde | renat: Fixed Translated
title_field replaces node->title with translated value.
Title 7.x-1.0-alpha7, 2013-03-18
--------------------------------
#1919640 by peximo: Fixed Empty tag shown when the title field is displayed.
@@ -0,0 +1,13 @@
Title is built and maintained by the Drupal project community.
Everyone is encouraged to submit issues and changes (patches) to improve it, and
to contribute in other ways -- see http://drupal.org/contribute to find out how.
Project maintainers
-------------------
The Title maintainers oversee the development of the project as a whole. The project
maintainers for Title are:
- Francesco Placella 'plach' <http://drupal.org/u/plach>, branch 7.x-1.x
- Sam Becker 'Sam152' <https://www.drupal.org/u/sam152>, branch 8.x-2.x
@@ -9,6 +9,7 @@
* Tests for legacy field replacement.
*/
class TitleFieldReplacementTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Field replacement',
@@ -17,14 +18,17 @@ class TitleFieldReplacementTestCase extends DrupalWebTestCase {
);
}
function setUp() {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp('entity', 'field_test', 'title', 'title_test');
}
/**
* Test field replacement API and workflow.
*/
function testFieldReplacementWorkflow() {
public function testFieldReplacementWorkflow() {
$info = entity_get_info('test_entity');
$label_key = $info['entity keys']['label'];
$field_name = $label_key . '_field';
@@ -72,12 +76,13 @@ class TitleFieldReplacementTestCase extends DrupalWebTestCase {
// Clear field cache so synchronization can be performed on field attach
// load.
cache_clear_all('*', 'cache_field');
drupal_static_reset();
// Check that the replacing field value is correctly synchronized on load
// and view.
$entity = title_test_entity_test_load($entity);
title_test_phase_check('after_load', $entity);
$build = entity_view('test_entity', array($entity->ftid => $entity));
entity_view('test_entity', array($entity->ftid => $entity));
foreach (title_test_phase_store() as $phase => $value) {
$this->assertTrue($value, t('Field synchronization is correctly performed on %phase.', array('%phase' => $phase)));
@@ -94,8 +99,16 @@ class TitleFieldReplacementTestCase extends DrupalWebTestCase {
/**
* Test field replacement UI.
*/
function testFieldReplacementUI() {
$admin_user = $this->drupalCreateUser(array('access administration pages', 'view the administration theme', 'administer content types', 'administer taxonomy', 'administer comments'));
public function testFieldReplacementUI() {
$permissions = array(
'access administration pages',
'view the administration theme',
'administer content types',
'administer taxonomy',
'administer comments',
'administer fields',
);
$admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($admin_user);
foreach (entity_get_info() as $entity_type => $entity_info) {
@@ -146,12 +159,14 @@ class TitleFieldReplacementTestCase extends DrupalWebTestCase {
}
}
}
}
/**
* Tests for legacy field replacement.
*/
class TitleAdminSettingsTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Admin settings',
@@ -160,7 +175,10 @@ class TitleAdminSettingsTestCase extends DrupalWebTestCase {
);
}
function setUp() {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp('field_test', 'title', 'title_test');
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy'));
$this->drupalLogin($admin_user);
@@ -169,15 +187,18 @@ class TitleAdminSettingsTestCase extends DrupalWebTestCase {
/**
* Check for automated title_field attachment.
*/
function testAutomatedFieldAttachement() {
$this->assertAutomatedFieldAttachement(TRUE);
$this->assertAutomatedFieldAttachement(FALSE);
public function testAutomatedFieldAttachment() {
$this->doTestAutomatedFieldAttachment(TRUE);
$this->doTestAutomatedFieldAttachment(FALSE);
}
/**
* Check that the fields are replaced or skipped depdening on the given value.
* Check that the fields are replaced or skipped depending on the given value.
*
* @param bool $enabled
* Whether replacement is enabled or not.
*/
function assertAutomatedFieldAttachement($enabled) {
public function doTestAutomatedFieldAttachment($enabled) {
$edit = array(
'title_taxonomy_term[auto_attach][name]' => $enabled,
'title_taxonomy_term[auto_attach][description]' => $enabled,
@@ -197,12 +218,14 @@ class TitleAdminSettingsTestCase extends DrupalWebTestCase {
$this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, 'name') == $enabled, 'Name field correctly processed.');
$this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, 'description') == $enabled, 'Description field correctly processed.');
}
}
/**
* Tests for legacy field replacement.
*/
class TitleTranslationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Replaced fields translation',
@@ -211,11 +234,22 @@ class TitleTranslationTestCase extends DrupalWebTestCase {
);
}
function setUp() {
parent::setUp('locale', 'entity_translation', 'title');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp('locale', 'entity_translation', 'title', 'field_test', 'title_test');
// Create a power user.
$admin_user = $this->drupalCreateUser(array('administer modules', 'view the administration theme', 'administer languages', 'administer taxonomy', 'administer entity translation', 'translate any entity'));
$permissions = array(
'administer modules',
'view the administration theme',
'administer languages',
'administer taxonomy',
'administer entity translation',
'translate any entity',
);
$admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($admin_user);
// Enable a translation language.
@@ -240,6 +274,7 @@ class TitleTranslationTestCase extends DrupalWebTestCase {
$edit = array(
'name' => $this->randomString(),
'machine_name' => $name,
'entity_translation_taxonomy' => 1,
);
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
$this->vocabulary = taxonomy_vocabulary_machine_name_load($name);
@@ -252,19 +287,118 @@ class TitleTranslationTestCase extends DrupalWebTestCase {
$t_args = array('%legacy_field' => $legacy_field);
$this->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], $name), t('The %legacy_field field has been correctly replaced.', $t_args));
}
// Ensure static caches do not interfere with API calls.
drupal_static_reset();
}
/**
* Test taxonomy translation workflow.
* Tests taxonomy programmatic translation workflow.
*/
function testTranslationWorkflow() {
public function testProgrammaticTranslationWorkflow() {
// Create a taxonomy term and assign it an original language different from
// the default language.
$langcode = 'it';
$original_values = array(
'name' => $langcode . '_' . $this->randomName(),
'description' => $langcode . '_' . $this->randomName(),
);
$term = (object) ($original_values + array(
'format' => 'filtered_html',
'vocabulary_machine_name' => $this->vocabulary->machine_name,
'vid' => $this->vocabulary->vid,
));
entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($langcode);
taxonomy_term_save($term);
$this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this->termLoad($term->tid);
$this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');
// Pollute synchronization cache to ensure the expected values are stored
// anyway.
title_entity_sync('taxonomy_term', $term, $langcode);
// Create a translation using the default language.
$translation_langcode = language_default()->language;
$translated_values = array(
'name' => $translation_langcode . '_' . $this->randomName(),
'description' => $translation_langcode . '_' . $this->randomName(),
);
foreach ($translated_values as $name => $value) {
$term->{$name} = $value;
}
$translation = array(
'language' => $translation_langcode,
'source' => $langcode,
'uid' => $this->loggedInUser->uid,
'status' => 1,
'translate' => 0,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
);
entity_translation_get_handler('taxonomy_term', $term)->setTranslation($translation);
taxonomy_term_save($term);
$this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this->termLoad($term->tid, $translation_langcode);
$this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode), 'Replacing field translations correctly created.');
$this->assertTrue($this->checkFieldValues($term, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');
// Delete the translation.
entity_translation_get_handler('taxonomy_term', $term)->removeTranslation($translation_langcode);
taxonomy_term_save($term);
$this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this->termLoad($term->tid, $langcode);
$this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field translations correctly deleted.');
// Make the term language neutral.
entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage(LANGUAGE_NONE);
foreach ($original_values as $name => $value) {
$field_name = $name . '_field';
$term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode];
$term->{$field_name}[$langcode] = array();
}
taxonomy_term_save($term);
$this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this->termLoad($term->tid);
$this->assertTrue($this->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');
// Change the term language to the former translation language.
entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($translation_langcode);
foreach ($original_values as $name => $value) {
$field_name = $name . '_field';
$term->{$field_name}[$translation_langcode] = $term->{$field_name}[LANGUAGE_NONE];
$term->{$field_name}[LANGUAGE_NONE] = array();
}
taxonomy_term_save($term);
$this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this->termLoad($term->tid, $translation_langcode);
$this->assertTrue($this->checkFieldValues($term, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');
// Make a replacing field untranslatable and change its value.
$field_name = 'name_field';
$field = field_info_field($field_name);
$field['translatable'] = FALSE;
field_update_field($field);
$original_values['name'] = LANGUAGE_NONE . '_' . $this->randomName();
$term->name = $original_values['name'];
taxonomy_term_save($term);
$this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
$term = $this->termLoad($term->tid);
$this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['name'], 'Untranslatable replacing field on translatable entity correctly handled.');
}
/**
* Tests taxonomy form translation workflow.
*/
public function testFormTranslationWorkflow() {
// Create a taxonomy term and check that legacy fields are properly
// populated.
$original_values = array(
'name' => $this->randomName(),
'description' => $this->randomName(),
);
$edit = $this->editValues($original_values, 'en');
$langcode = 'en';
$edit = $this->editValues($original_values, $langcode);
$this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
$term = current(entity_load('taxonomy_term', FALSE, array('name' => $original_values['name']), TRUE));
$this->assertEqual($term->description, $original_values['description'], t('Taxonomy term created.'));
@@ -275,11 +409,12 @@ class TitleTranslationTestCase extends DrupalWebTestCase {
'name' => $this->randomName(),
'description' => $this->randomName(),
);
$translation_langcode = 'it';
$edit = $this->editValues($translated_values, 'it');
$this->drupalPost('it/taxonomy/term/' . $term->tid . '/edit/add/en/it', $edit, t('Save'));
$term = current(entity_load('taxonomy_term', array($term->tid), array(), TRUE));
$this->assertTrue($this->checkFieldValues($term, $translated_values, 'it'), t('Taxonomy term translation created.'));
$this->assertTrue($this->checkFieldValues($term, $original_values, 'en'), t('Taxonomy term original values preserved.'));
$this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save'));
$term = $this->termLoad($term->tid);
$this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation created.'));
$this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));
// Check that legacy fields have the correct values.
$this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
@@ -291,31 +426,69 @@ class TitleTranslationTestCase extends DrupalWebTestCase {
'name' => $this->randomName(),
'description' => $this->randomName(),
);
$edit = $this->editValues($translated_values, 'it');
$this->drupalPost('it/taxonomy/term/' . $term->tid . '/edit/it', $edit, t('Save'));
$term = current(entity_load('taxonomy_term', array($term->tid), array(), TRUE));
$this->assertTrue($this->checkFieldValues($term, $translated_values, 'it'), t('Taxonomy term translation updated.'));
$this->assertTrue($this->checkFieldValues($term, $original_values, 'en'), t('Taxonomy term original values preserved.'));
$edit = $this->editValues($translated_values, $translation_langcode);
$this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'));
$term = $this->termLoad($term->tid);
$this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.'));
$this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));
// Check that legacy fields have the correct values.
$this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
$this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
}
/**
* Loads a term using the given language as active language.
*
* @param int $tid
* The term identifier.
* @param string|null $langcode
* (optional) The active language to be set. Defaults to none.
*
* @return object|bool
* A term object.
*/
protected function termLoad($tid, $langcode = NULL) {
drupal_static_reset();
title_active_language($langcode);
return current(entity_load('taxonomy_term', array($tid), array(), TRUE));
}
/**
* Returns the drupalPost() $edit array corresponding to the given values.
*/
protected function editValues($values, $langcode) {
$edit = array();
foreach ($values as $key => $value) {
$edit["{$key}_field[{$langcode}][0][value]"] = $value;
foreach ($values as $name => $value) {
$edit["{$name}_field[{$langcode}][0][value]"] = $value;
}
return $edit;
}
protected function checkFieldValues($term, $values, $langcode) {
foreach ($values as $key => $value) {
if ($term->{$key . '_field'}[$langcode][0]['value'] != $value) {
/**
* Checks that the field values and optionally the legacy ones match the given values.
*/
protected function checkFieldValues($term, $values, $langcode, $legacy_match = TRUE) {
foreach ($values as $name => $value) {
$field_value = $term->{$name . '_field'}[$langcode][0]['value'];
if ($field_value != $value || ($legacy_match !== ($field_value == $term->{$name}))) {
return FALSE;
}
}
return TRUE;
}
/**
* Checks that the legacy field values stored in the database match the given values.
*/
protected function checkLegacyValues($term, $values) {
$record = db_query('SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid', array(':tid' => $term->tid))->fetchAssoc();
foreach ($values as $name => $value) {
if ($record[$name] != $value) {
return FALSE;
}
}
return TRUE;
}
}
@@ -7,9 +7,9 @@ dependencies[] = title
dependencies[] = entity
dependencies[] = entity_translation
; Information added by drupal.org packaging script on 2013-03-18
version = "7.x-1.0-alpha7"
; Information added by Drupal.org packaging script on 2017-01-13
version = "7.x-1.0-alpha9"
core = "7.x"
project = "title"
datestamp = "1363626024"
datestamp = "1484302985"
@@ -14,20 +14,16 @@ function title_form_field_ui_overview(&$form, &$form_state) {
if (!empty($entity_info['field replacement'])) {
$field_replacement_info = $entity_info['field replacement'];
$admin_path = _field_ui_bundle_admin_path($form['#entity_type'], $form['#bundle']);
$form['fields']['#header'][6]['colspan'] += 1;
foreach (element_children($form['fields']) as $field_name) {
if (isset($field_replacement_info[$field_name])) {
$form['fields'][$field_name]['field_replacement'] = array(
$form['fields'][$field_name]['delete'] = array(
'#type' => 'link',
'#title' => t('replace'),
'#href' => $admin_path . '/fields/replace/' . $field_name,
'#options' => array('attributes' => array('title' => t('Replace %field with a customizable field instance that can be translated.', array('%field' => $field_name)))),
);
}
else {
$form['fields'][$field_name]['field_replacement'] = array();
}
}
}
}
@@ -127,7 +127,7 @@ function title_field_text_sync_get($entity_type, $entity, $legacy_field, $info,
$items = $entity->{$field_name}[$langcode];
$value = !empty($items[0]['value']) ? $items[0]['value'] : NULL;
}
return $value;
return array($legacy_field => $value);
}
/**
@@ -142,25 +142,31 @@ function title_field_text_sync_set($entity_type, $entity, $legacy_field, $info,
*/
function title_field_text_with_summary_sync_get($entity_type, $entity, $legacy_field, $info, $langcode) {
$value = NULL;
$format = NULL;
$format_key = $info['additional keys']['format'];
$field_name = $info['field']['field_name'];
// Return values only if there is any available to process for the current
// language.
if (!empty($entity->{$field_name}[$langcode]) && is_array($entity->{$field_name}[$langcode])) {
$entity->{$format_key} = $entity->{$field_name}[$langcode][0]['format'];
$items = $entity->{$field_name}[$langcode];
$value = !empty($items[0]['value']) ? $items[0]['value'] : NULL;
$format = $entity->{$field_name}[$langcode][0]['format'];
}
return $value;
return array(
$legacy_field => $value,
$format_key => $format,
);
}
/**
* Sync back callback for the text with summary field type.
*/
function title_field_text_with_summary_sync_set($entity_type, $entity, $legacy_field, $info, $langcode) {
$format_key = $info['additional keys']['format'];
$entity->{$info['field']['field_name']}[$langcode][0]['value'] = $entity->{$legacy_field};
$entity->{$info['field']['field_name']}[$langcode][0]['format'] = $entity->{$format_key};
foreach (array('value' => $legacy_field, 'format' => $info['additional keys']['format']) as $column => $name) {
if (isset($entity->{$name})) {
$entity->{$info['field']['field_name']}[$langcode][0][$column] = $entity->{$name};
}
}
}
/**
@@ -97,36 +97,48 @@ function title_field_formatter_settings_summary($field, $instance, $view_mode) {
*/
function title_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$output = isset($items[0]) ? $items[0]['safe_value'] : '';
if (!empty($output) && $settings['title_link'] == 'content') {
$uri = entity_uri($entity_type, $entity);
$output = l($output, $uri['path'], array('html' => TRUE));
$output = '';
if (isset($items[0]['safe_value'])) {
$output = $items[0]['safe_value'];
}
elseif (isset($items[0]['value'])) {
$output = _text_sanitize($instance, $langcode, $items[0], 'value');
}
$element = array();
$wrap_tag = empty($settings['title_style']) ? '_none' : $settings['title_style'];
if ($wrap_tag != '_none') {
$element = array(
'element' => array(
'#tag' => $wrap_tag,
'#value' => $output,
),
);
if (!empty($settings['title_class'])) {
$element['#attributes'] = array('class' => $settings['title_class']);
if (!empty($output)) {
if ($settings['title_link'] == 'content') {
$uri = entity_uri($entity_type, $entity);
$options = array('html' => TRUE);
if (!empty($uri['options'])) {
$options = array_merge($options, $uri['options']);
}
$output = l($output, $uri['path'], $options);
}
$output = theme('html_tag', $element);
$wrap_tag = empty($settings['title_style']) ? '_none' : $settings['title_style'];
if ($wrap_tag != '_none') {
$variables = array(
'element' => array(
'#tag' => $wrap_tag,
'#value' => $output,
),
);
if (!empty($settings['title_class'])) {
$variables['element']['#attributes'] = array('class' => $settings['title_class']);
}
$output = theme('html_tag', $variables);
}
$element = array(
array(
'#markup' => $output,
),
);
}
$element = array(
array(
'#markup' => $output,
),
);
return $element;
}
@@ -9,9 +9,9 @@ files[] = title.module
files[] = views/views_handler_title_field.inc
files[] = tests/title.test
; Information added by drupal.org packaging script on 2013-03-18
version = "7.x-1.0-alpha7"
; Information added by Drupal.org packaging script on 2017-01-13
version = "7.x-1.0-alpha9"
core = "7.x"
project = "title"
datestamp = "1363626024"
datestamp = "1484302985"
@@ -33,6 +33,7 @@ function title_module_implements_alter(&$implementations, $hook) {
// The following hook implementations should be executed as last ones.
case 'entity_info_alter':
case 'entity_presave':
case 'field_attach_presave':
$implementations['title'] = $group;
break;
@@ -104,17 +105,7 @@ function title_field_replacement_info($entity_type, $legacy_field = NULL) {
}
/**
* Return an entity label value.
*
* @param $entity
* The entity whose label has to be displayed.
* @param $type
* The name of the entity type.
* @param $langcode
* (Optional) The language the entity label has to be displayed in.
*
* @return
* The entity label as a string value.
* Implements callback_entity_info_label().
*/
function title_entity_label($entity, $type, $langcode = NULL) {
$entity_info = entity_get_info($type);
@@ -125,7 +116,8 @@ function title_entity_label($entity, $type, $langcode = NULL) {
// If field replacement is enabled we use the replacing field value.
if (title_field_replacement_enabled($type, $bundle, $legacy_field)) {
$langcode = field_language($type, $entity, $info['field']['field_name'], $langcode);
return $info['callbacks']['sync_get']($type, $entity, $legacy_field, $info, $langcode);
$values = $info['callbacks']['sync_get']($type, $entity, $legacy_field, $info, $langcode);
return $values[$legacy_field];
}
// Otherwise if we have a fallback defined we use the original label callback.
elseif (isset($entity_info['label fallback']['title']) && function_exists($entity_info['label fallback']['title'])) {
@@ -139,8 +131,57 @@ function title_entity_label($entity, $type, $langcode = NULL) {
/**
* Implements hook_entity_presave().
*/
function title_entity_presave($entity, $type) {
title_entity_sync($type, $entity, NULL, TRUE);
function title_entity_presave($entity, $entity_type) {
$entity_langcode = title_entity_language($entity_type, $entity);
$langcode = $entity_langcode;
// If Entity Translation is enabled and the entity type is transltable,we need
// to check if we have a translation for the current active language. If so we
// need to synchronize the legacy field values into the replacing field
// translations in the active language.
if (module_invoke('entity_translation', 'enabled', $entity_type)) {
$langcode = title_active_language();
$translations = entity_translation_get_handler($entity_type, $entity)->getTranslations();
// If we are removing a translation for the active language we need to skip
// reverse synchronization, as we would store empty values in the original
// replacing fields immediately afterwards.
if (!isset($translations->data[$langcode])) {
$langcode = isset($translations->hook[$langcode]['hook']) && $translations->hook[$langcode]['hook'] == 'delete' ? FALSE : $entity_langcode;
}
}
// Perform reverse synchronization to retain any change in the legacy field
// values. We must avoid doing this twice as we might overwrite the already
// synchronized values, if we are updating an existing entity.
if ($langcode) {
title_entity_sync($entity_type, $entity, $langcode, TRUE);
}
// If we are not dealing with the entity language, we need to synchronize the
// original values into the legacy fields to ensure they are always stored in
// the entity table.
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);
}
}
/**
* Implements hook_field_attach_update().
*/
function title_field_attach_update($entity_type, $entity) {
// Reset the field_attach_presave static cache so that subsequent saves work
// correctly.
$sync = &drupal_static('title_field_attach_presave', array());
list($id, , ) = entity_extract_ids($entity_type, $entity);
unset($sync[$entity_type][$id]);
// Immediately after saving the entity we need to ensure that the legacy field
// holds a value corresponding to the current active language, as it were
// just loaded.
title_entity_sync($entity_type, $entity);
}
/**
@@ -152,7 +193,10 @@ function title_entity_presave($entity, $type) {
* @see title_entity_load()
*/
function title_field_attach_load($entity_type, $entities, $age, $options) {
// @todo: Do we need to handle revisions here?
// Allow values to re-sync when field_attach_load_revision() is called.
if ($age == FIELD_LOAD_REVISION) {
title_entity_sync_static_reset($entity_type, array_keys($entities));
}
title_entity_load($entities, $entity_type);
}
@@ -164,6 +208,9 @@ function title_field_attach_load($entity_type, $entities, $age, $options) {
* replaced fields.
*/
function title_entity_load($entities, $type) {
// Load entity translations otherwise field language will not be computed
// correctly.
module_invoke('entity_translation', 'entity_load', $entities, $type);
foreach ($entities as &$entity) {
// Synchronize values from the regular field unless we are intializing it.
title_entity_sync($type, $entity, NULL, !empty($GLOBALS['title_field_replacement_init']));
@@ -183,20 +230,10 @@ function title_entitycache_load($entities, $type) {
/**
* Implements hook_entitycache_reset().
*
* If the entity cache is reseted the field sync has to be done again.
* When the entity cache is reset the field sync has to be done again.
*/
function title_entitycache_reset($ids, $entity_type) {
$sync = &drupal_static('title_entity_sync', array());
if (!empty($ids)) {
// Clear specific ids.
foreach ($ids as $id) {
unset($sync[$entity_type][$id]);
}
}
elseif (!empty($sync[$entity_type])) {
// Reset cache for an entity_type.
$sync[$entity_type] = array();
}
title_entity_sync_static_reset($entity_type, $ids);
}
/**
@@ -227,7 +264,9 @@ function title_entity_prepare_view($entities, $type, $langcode) {
*/
function title_field_replacement_enabled($entity_type, $bundle, $legacy_field) {
$info = title_field_replacement_info($entity_type, $legacy_field);
$instance = field_info_instance($entity_type, $info['field']['field_name'], $bundle);
if (!empty($info['field']['field_name'])) {
$instance = field_info_instance($entity_type, $info['field']['field_name'], $bundle);
}
return !empty($instance);
}
@@ -374,10 +413,13 @@ function title_field_replacement_init($entity_type, $bundle, $legacy_field, $ids
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);
$langcode = field_valid_language($langcode, FALSE);
// We do not need to perform this more than once.
if (!empty($id) && !empty($sync[$entity_type][$id][$langcode][$set])) {
if (!isset($langcode)) {
$langcode = $set ? title_entity_language($entity_type, $entity) : title_active_language();
}
// We do not need to perform synchronization more than once.
if (!$set && !empty($id) && !empty($sync[$entity_type][$id][$langcode][$set])) {
return;
}
@@ -394,6 +436,26 @@ function title_entity_sync($entity_type, &$entity, $langcode = NULL, $set = FALS
}
}
/**
* Reset the list of entities whose fields have already been synchronized.
*
* @param $entity_type
* The name of the entity type.
* @param $entity_ids
* Either an array of entity IDs to reset or NULL to reset all.
*/
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]);
}
}
/**
* Synchronize a single legacy field with its regular field value.
*
@@ -405,8 +467,8 @@ function title_entity_sync($entity_type, &$entity, $langcode = NULL, $set = FALS
* The name of the legacy field to be replaced.
* @param $field_name
* The regular field to use as source value.
* @param $display
* Specifies if synchronization is being performed on display or on save.
* @param $info
* Field replacement information for the given entity.
* @param $langcode
* The field language to use for the source value.
*/
@@ -416,7 +478,16 @@ function title_field_sync_get($entity_type, $entity, $legacy_field, $info, $lang
$entity->{$legacy_field . '_original'} = $entity->{$legacy_field};
// Find out the actual language to use (field might be untranslatable).
$langcode = field_language($entity_type, $entity, $info['field']['field_name'], $langcode);
$entity->{$legacy_field} = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
$values = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
foreach ($values as $name => $value) {
if ($value !== NULL) {
$entity->{$name} = $value;
}
}
// Ensure we do not pollute field language static cache.
$cache = &drupal_static('field_language');
list($id, ,) = entity_extract_ids($entity_type, $entity);
unset($cache[$entity_type][$id]);
}
}
@@ -431,18 +502,45 @@ function title_field_sync_get($entity_type, $entity, $legacy_field, $info, $lang
* The name of the legacy field to be replaced.
* @param $field_name
* The regular field to use as source value.
* @param $display
* Specifies if synchronization is being performed on display or on save.
* @param $info
* Field replacement information for the given entity.
* @param $langcode
* The field language to use for the source value.
* The field language to use for the target value.
*/
function title_field_sync_set($entity_type, $entity, $legacy_field, $info) {
function title_field_sync_set($entity_type, $entity, $legacy_field, $info, $langcode) {
if (property_exists($entity, $legacy_field)) {
$langcode = title_entity_language($entity_type, $entity);
// Find out the actual language to use (field might be untranslatable).
$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);
}
}
/**
* Returns and optionally stores the active language.
*
* @param string $langcode
* (optional) The active language to be set. If none is provided the active
* language is just returned.
*
* @return string
* The active language code. Defaults to the current content language.
*/
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;
}
/**
* Provide the original entity language.
*
@@ -485,6 +583,11 @@ function title_field_attach_form($entity_type, $entity, &$form, &$form_state, $l
if (isset($form[$legacy_field]['#access'])) {
$form[$info['field']['field_name']]['#access'] = $form[$legacy_field]['#access'];
}
// Add class from legacy field so behaviors can still be applied on
// title widget.
$form[$info['field']['field_name']]['#attributes']['class'] = array('form-item-' . $legacy_field);
// Restrict access to the legacy field form element and mark it as
// replaced.
$form[$legacy_field]['#access'] = FALSE;
@@ -510,7 +613,7 @@ function title_field_attach_submit($entity_type, $entity, $form, &$form_state) {
// drupal_array_set_nested_value().
$values = $form_state['values'];
$values = drupal_array_get_nested_value($values, $form['#parents']);
$langcode = title_entity_language($entity_type, $entity);
$langcode = entity_language($entity_type, $entity);
foreach ($fr_info as $legacy_field => $info) {
if (!empty($form[$legacy_field]['#field_replacement'])) {
@@ -641,23 +744,26 @@ function title_tokens_alter(array &$replacements, array $context) {
$entity = $context['data'][$context['type']];
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
$options = $context['options'];
$sanitize = !empty($options['sanitize']);
$langcode = NULL;
if (isset($options['language'])) {
$langcode = $options['language']->language;
}
// Since Title tokens are mostly used in storage contexts we default to
// the current working language, that is the entity language. Modules
// using Title tokens in display contexts need to specify the current
// display language.
$langcode = isset($options['language']) ? $options['language']->language : entity_language($entity_type, $entity);
if ($fr_info) {
foreach ($fr_info as $legacy_field => $info) {
if (title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
if (isset($context['tokens'][$legacy_field])) {
title_field_sync_get($entity_type, $entity, $legacy_field, $info, $langcode);
$item = $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);
$item = $values[$legacy_field];
if (!empty($item)) {
if (is_array($item)) {
$item = reset($item);
}
$replacements[$context['tokens'][$legacy_field]] = $item;
$replacements[$context['tokens'][$legacy_field]] = $sanitize ? check_plain($item) : $item;
}
}
}
@@ -851,3 +957,38 @@ function title_field_attach_create_bundle($entity_type, $bundle) {
}
}
}
/**
* Implements hook_field_info_alter().
*/
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');
}
}
}
/**
* Return taxonomy term values for taxonomy reference fields.
*/
function title_taxonomy_allowed_values($field) {
$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))) {
$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);
}
@@ -6,13 +6,14 @@
*/
function title_field_views_data_alter(&$data) {
foreach (entity_get_info() as $entity_type => $entity_info) {
$replacements = title_field_replacement_info($entity_type);
if ($replacements) {
foreach ($replacements as $replacement) {
$field = field_info_field($replacement['field']['field_name']);
$table = _field_sql_storage_tablename($field);
if (isset($data[$table][$field['field_name']])) {
$data[$table][$field['field_name']]['field']['handler'] = 'views_handler_title_field';
if (!empty($entity_info['field replacement'])) {
foreach ($entity_info['field replacement'] as $replacement) {
if (isset($replacement['field']['field_name'])) {
$field = field_info_field($replacement['field']['field_name']);
$table = _field_sql_storage_tablename($field);
if (isset($data[$table][$field['field_name']])) {
$data[$table][$field['field_name']]['field']['handler'] = 'views_handler_title_field';
}
}
}
}
@@ -36,13 +36,13 @@ class views_handler_title_field extends views_handler_field_field {
if (!empty($this->options['link_to_entity'])) {
$values = $this->original_values;
$entity_type = $this->definition['entity_tables'][$this->base_table];
$entity_info = entity_get_info($entity_type);
$key = $entity_info['entity keys']['id'];
$key = $this->field_alias;
if (!empty($values->_field_data[$key]['entity'])) {
$entity = $values->_field_data[$key]['entity'];
$uri = entity_uri($entity_type, $entity);
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = $uri['path'];
$this->options['alter']['options'] = !empty($uri['options']) ? $uri['options'] : array();
}
}
return parent::render_item($count, $item);