title.module 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. <?php
  2. /**
  3. * @file
  4. * Replaces entity legacy fields with regular fields.
  5. *
  6. * Provides an API and a basic UI to replace legacy pseudo-fields with regular
  7. * fields. The API only offers synchronization between the two data storage
  8. * systems and data replacement on entity load/save. Field definitions have to
  9. * be provided by the modules exploiting the API.
  10. *
  11. * Title implements its own entity description API to describe core legacy
  12. * pseudo-fields:
  13. * - Node: title
  14. * - Taxonomy Term: name, description
  15. * - Comment: subject
  16. *
  17. * @todo: API PHPdocs
  18. */
  19. module_load_include('inc', 'title', 'title.core');
  20. module_load_include('inc', 'title', 'title.field');
  21. /**
  22. * Implements hook_module_implements_alter().
  23. */
  24. function title_module_implements_alter(&$implementations, $hook) {
  25. if (isset($implementations['title'])) {
  26. $group = $implementations['title'];
  27. unset($implementations['title']);
  28. switch ($hook) {
  29. // The following hook implementations should be executed as last ones.
  30. case 'entity_info_alter':
  31. case 'entity_presave':
  32. case 'field_attach_presave':
  33. $implementations['title'] = $group;
  34. break;
  35. // Normally Title needs to act as first module to perform synchronization.
  36. default:
  37. $implementations = array('title' => $group) + $implementations;
  38. }
  39. }
  40. }
  41. /**
  42. * Implements hook_entity_info_alter().
  43. */
  44. function title_entity_info_alter(&$info) {
  45. foreach ($info as $entity_type => $entity_info) {
  46. if (!empty($entity_info['fieldable']) && !empty($info[$entity_type]['field replacement'])) {
  47. foreach ($info[$entity_type]['field replacement'] as $legacy_field => $data) {
  48. // Provide defaults for the replacing field name.
  49. $fr_info = &$info[$entity_type]['field replacement'][$legacy_field];
  50. if (empty($fr_info['field']['field_name'])) {
  51. $fr_info['field']['field_name'] = $legacy_field . '_field';
  52. }
  53. $fr_info['instance']['field_name'] = $fr_info['field']['field_name'];
  54. // Provide defaults for the sync callbacks.
  55. $type = $fr_info['field']['type'];
  56. if (empty($fr_info['callbacks'])) {
  57. $fr_info['callbacks'] = array();
  58. }
  59. $fr_info['callbacks'] += array(
  60. 'sync_get' => "title_field_{$type}_sync_get",
  61. 'sync_set' => "title_field_{$type}_sync_set",
  62. );
  63. // Support add explicit support for entity_label().
  64. if (isset($entity_info['entity keys']['label']) && $entity_info['entity keys']['label'] == $legacy_field) {
  65. // Store the original label callback for compatibility reasons.
  66. if (isset($info[$entity_type]['label callback'])) {
  67. $info[$entity_type]['label fallback']['title'] = $info[$entity_type]['label callback'];
  68. }
  69. $info[$entity_type]['label callback'] = 'title_entity_label';
  70. $fr_info += array('preprocess_key' => $info[$entity_type]['entity keys']['label']);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * Return field replacement specific information.
  78. *
  79. * @param $entity_type
  80. * The name of the entity type.
  81. * @param $legacy_field
  82. * (Otional) The legacy field name to be replaced.
  83. */
  84. function title_field_replacement_info($entity_type, $legacy_field = NULL) {
  85. $info = entity_get_info($entity_type);
  86. if (empty($info['field replacement'])) {
  87. return FALSE;
  88. }
  89. if (isset($legacy_field)) {
  90. return isset($info['field replacement'][$legacy_field]) ? $info['field replacement'][$legacy_field] : FALSE;
  91. }
  92. else {
  93. return $info['field replacement'];
  94. }
  95. }
  96. /**
  97. * Return an entity label value.
  98. *
  99. * @param $entity
  100. * The entity whose label has to be displayed.
  101. * @param $type
  102. * The name of the entity type.
  103. * @param $langcode
  104. * (Optional) The language the entity label has to be displayed in.
  105. *
  106. * @return
  107. * The entity label as a string value.
  108. */
  109. function title_entity_label($entity, $type, $langcode = NULL) {
  110. $entity_info = entity_get_info($type);
  111. $legacy_field = $entity_info['entity keys']['label'];
  112. $info = $entity_info['field replacement'][$legacy_field];
  113. list(, , $bundle) = entity_extract_ids($type, $entity);
  114. // If field replacement is enabled we use the replacing field value.
  115. if (title_field_replacement_enabled($type, $bundle, $legacy_field)) {
  116. $langcode = field_language($type, $entity, $info['field']['field_name'], $langcode);
  117. $values = $info['callbacks']['sync_get']($type, $entity, $legacy_field, $info, $langcode);
  118. return $values[$legacy_field];
  119. }
  120. // Otherwise if we have a fallback defined we use the original label callback.
  121. elseif (isset($entity_info['label fallback']['title']) && function_exists($entity_info['label fallback']['title'])) {
  122. return $entity_info['label fallback']['title']($entity, $type, $langcode);
  123. }
  124. else {
  125. return (property_exists($entity, $legacy_field)) ? $entity->{$legacy_field} : NULL;
  126. }
  127. }
  128. /**
  129. * Implements hook_entity_presave().
  130. */
  131. function title_entity_presave($entity, $entity_type) {
  132. $entity_langcode = title_entity_language($entity_type, $entity);
  133. $langcode = $entity_langcode;
  134. // If Entity Translation is enabled and the entity type is transltable,we need
  135. // to check if we have a translation for the current active language. If so we
  136. // need to synchronize the legacy field values into the replacing field
  137. // translations in the active language.
  138. if (module_invoke('entity_translation', 'enabled', $entity_type)) {
  139. $langcode = title_active_language();
  140. $translations = entity_translation_get_handler($entity_type, $entity)->getTranslations();
  141. // If we are removing a translation for the active language we need to skip
  142. // reverse synchronization, as we would store empty values in the original
  143. // replacing fields immediately afterwards.
  144. if (!isset($translations->data[$langcode])) {
  145. $langcode = isset($translations->hook[$langcode]['hook']) && $translations->hook[$langcode]['hook'] == 'delete' ? FALSE : $entity_langcode;
  146. }
  147. }
  148. // Perform reverse synchronization to retain any change in the legacy field
  149. // values. We must avoid doing this twice as we might overwrite the already
  150. // synchronized values, if we are updating an existing entity.
  151. if ($langcode) {
  152. title_entity_sync($entity_type, $entity, $langcode, TRUE);
  153. }
  154. // If we are not dealing with the entity language, we need to synchronize the
  155. // original values into the legacy fields to ensure they are always stored in
  156. // the entity table.
  157. if ($entity_langcode != $langcode) {
  158. list($id, , ) = entity_extract_ids($entity_type, $entity);
  159. $sync = &drupal_static('title_entity_sync', array());
  160. unset($sync[$entity_type][$id]);
  161. title_entity_sync($entity_type, $entity, $entity_langcode);
  162. }
  163. }
  164. /**
  165. * Implements hook_field_attach_update().
  166. */
  167. function title_field_attach_update($entity_type, $entity) {
  168. // Reset the field_attach_presave static cache so that subsequent saves work
  169. // correctly.
  170. $sync = &drupal_static('title_field_attach_presave', array());
  171. list($id, , ) = entity_extract_ids($entity_type, $entity);
  172. unset($sync[$entity_type][$id]);
  173. // Immediately after saving the entity we need to ensure that the legacy field
  174. // holds a value corresponding to the current active language, as it were
  175. // just loaded.
  176. title_entity_sync($entity_type, $entity);
  177. }
  178. /**
  179. * Implements hook_field_attach_load().
  180. *
  181. * Synchronization must be performed as early as possible to prevent other code
  182. * from accessing replaced fields before they get their actual value.
  183. *
  184. * @see title_entity_load()
  185. */
  186. function title_field_attach_load($entity_type, $entities, $age, $options) {
  187. // @todo: Do we need to handle revisions here?
  188. title_entity_load($entities, $entity_type);
  189. }
  190. /**
  191. * Implements hook_entity_load().
  192. *
  193. * Since the result of field_attach_load() is cached, synchronization must be
  194. * performed also here to ensure that there is always the correct value in the
  195. * replaced fields.
  196. */
  197. function title_entity_load($entities, $type) {
  198. foreach ($entities as &$entity) {
  199. // Synchronize values from the regular field unless we are intializing it.
  200. title_entity_sync($type, $entity, NULL, !empty($GLOBALS['title_field_replacement_init']));
  201. }
  202. }
  203. /**
  204. * Implements hook_entitycache_load().
  205. *
  206. * Entity cache might cache the entire $entity object, in which case
  207. * synchronization will not be performed on entity load.
  208. */
  209. function title_entitycache_load($entities, $type) {
  210. title_entity_load($entities, $type);
  211. }
  212. /**
  213. * Implements hook_entitycache_reset().
  214. *
  215. * If the entity cache is reseted the field sync has to be done again.
  216. */
  217. function title_entitycache_reset($ids, $entity_type) {
  218. $sync = &drupal_static('title_entity_sync', array());
  219. if (!empty($ids)) {
  220. // Clear specific ids.
  221. foreach ($ids as $id) {
  222. unset($sync[$entity_type][$id]);
  223. }
  224. }
  225. elseif (!empty($sync[$entity_type])) {
  226. // Reset cache for an entity_type.
  227. $sync[$entity_type] = array();
  228. }
  229. }
  230. /**
  231. * Implements hook_entity_prepare_view().
  232. *
  233. * On load synchronization is performed using the current display language. A
  234. * different language might be specified while viewing the entity in which case
  235. * synchronization must be performed again.
  236. */
  237. function title_entity_prepare_view($entities, $type, $langcode) {
  238. foreach ($entities as &$entity) {
  239. title_entity_sync($type, $entity, $langcode);
  240. }
  241. }
  242. /**
  243. * Check whether field replacement is enabled for the given field.
  244. *
  245. * @param $entity_type
  246. * The type of $entity.
  247. * @param $bundle
  248. * The bundle the legacy field belongs to.
  249. * @param $legacy_field
  250. * The name of the legacy field to be replaced.
  251. *
  252. * @return
  253. * TRUE if field replacement is enabled for the given field, FALSE otherwise.
  254. */
  255. function title_field_replacement_enabled($entity_type, $bundle, $legacy_field) {
  256. $info = title_field_replacement_info($entity_type, $legacy_field);
  257. if (!empty($info['field']['field_name'])) {
  258. $instance = field_info_instance($entity_type, $info['field']['field_name'], $bundle);
  259. }
  260. return !empty($instance);
  261. }
  262. /**
  263. * Toggle field replacement for the given field.
  264. *
  265. * @param $entity_type
  266. * The name of the entity type.
  267. * @param $bundle
  268. * The bundle the legacy field belongs to.
  269. * @param $legacy_field
  270. * The name of the legacy field to be replaced.
  271. */
  272. function title_field_replacement_toggle($entity_type, $bundle, $legacy_field) {
  273. $info = title_field_replacement_info($entity_type, $legacy_field);
  274. if (!$info) {
  275. return;
  276. }
  277. $field_name = $info['field']['field_name'];
  278. $instance = field_info_instance($entity_type, $field_name, $bundle);
  279. if (empty($instance)) {
  280. $options = variable_get('title_' . $entity_type, array());
  281. $field = field_info_field($field_name);
  282. if (empty($field)) {
  283. field_create_field($info['field']);
  284. }
  285. $info['instance']['entity_type'] = $entity_type;
  286. $info['instance']['bundle'] = $bundle;
  287. $info['instance']['settings']['hide_label']['page'] = isset($options['hide_label']['page']) ? $options['hide_label']['page'] : FALSE;
  288. $info['instance']['settings']['hide_label']['entity'] = isset($options['hide_label']['entity']) ? $options['hide_label']['entity'] : FALSE;
  289. field_create_instance($info['instance']);
  290. return TRUE;
  291. }
  292. else {
  293. field_delete_instance($instance);
  294. return FALSE;
  295. }
  296. }
  297. /**
  298. * Set a batch process to initialize replacing field values.
  299. *
  300. * @param $entity_type
  301. * The type of $entity.
  302. * @param $bundle
  303. * The bundle the legacy field belongs to.
  304. * @param $legacy_field
  305. * The name of the legacy field to be replaced.
  306. */
  307. function title_field_replacement_batch_set($entity_type, $bundle, $legacy_field) {
  308. $batch = array(
  309. 'title' => t('Replacing field values for %field', array('%field' => $legacy_field)),
  310. 'operations' => array(
  311. array('title_field_replacement_batch', array($entity_type, $bundle, $legacy_field)),
  312. ),
  313. );
  314. batch_set($batch);
  315. }
  316. /**
  317. * Batch operation: initialize a batch of replacing field values.
  318. */
  319. function title_field_replacement_batch($entity_type, $bundle, $legacy_field, &$context) {
  320. $info = entity_get_info($entity_type);
  321. $query = new EntityFieldQuery();
  322. $query->entityCondition('entity_type', $entity_type);
  323. // There is no general way to tell if an entity supports bundle conditions
  324. // (for instance taxonomy terms and comments do not), hence we may need to
  325. // loop over all the entities of the given type.
  326. if (!empty($info['efq bundle conditions'])) {
  327. $query->entityCondition('bundle', $bundle);
  328. }
  329. if (empty($context['sandbox'])) {
  330. $count_query = clone $query;
  331. $total = $count_query
  332. ->count()
  333. ->execute();
  334. $context['sandbox']['steps'] = 0;
  335. $context['sandbox']['progress'] = 0;
  336. $context['sandbox']['total'] = $total;
  337. }
  338. $step = variable_get('title_field_replacement_batch_size', 5);
  339. $start = $step * $context['sandbox']['steps']++;
  340. $results = $query
  341. ->entityCondition('entity_type', $entity_type)
  342. ->range($start, $step)
  343. ->execute();
  344. if (!empty($results[$entity_type])) {
  345. $ids = array_keys($results[$entity_type]);
  346. title_field_replacement_init($entity_type, $bundle, $legacy_field, $ids);
  347. $context['sandbox']['progress'] += count($ids);
  348. }
  349. if ($context['sandbox']['progress'] != $context['sandbox']['total']) {
  350. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['total'];
  351. }
  352. }
  353. /**
  354. * Initialize a batch of replacing field values.
  355. *
  356. * @param $entity_type
  357. * The type of $entity.
  358. * @param $bundle
  359. * The bundle the legacy field belongs to.
  360. * @param $legacy_field
  361. * The name of the legacy field to be replaced.
  362. * @param $ids
  363. * An array of entity IDs.
  364. *
  365. * @return
  366. * The number of entities processed.
  367. */
  368. function title_field_replacement_init($entity_type, $bundle, $legacy_field, $ids) {
  369. $GLOBALS['title_field_replacement_init'] = TRUE;
  370. $entities = entity_load($entity_type, $ids);
  371. foreach ($entities as $id => $entity) {
  372. list(, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  373. if ($entity_bundle == $bundle) {
  374. field_attach_presave($entity_type, $entity);
  375. field_attach_update($entity_type, $entity);
  376. }
  377. }
  378. unset($GLOBALS['title_field_replacement_init']);
  379. }
  380. /**
  381. * Synchronize replaced fields with the regular field values.
  382. *
  383. * @param $entity_type
  384. * The name of the entity type.
  385. * @param $entity
  386. * The entity to work with.
  387. * @param $set
  388. * Specifies the direction synchronization must be performed.
  389. */
  390. function title_entity_sync($entity_type, &$entity, $langcode = NULL, $set = FALSE) {
  391. $sync = &drupal_static(__FUNCTION__, array());
  392. list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  393. if (!isset($langcode)) {
  394. $langcode = $set ? title_entity_language($entity_type, $entity) : title_active_language();
  395. }
  396. // We do not need to perform synchronization more than once.
  397. if (!$set && !empty($id) && !empty($sync[$entity_type][$id][$langcode][$set])) {
  398. return;
  399. }
  400. $sync[$entity_type][$id][$langcode][$set] = TRUE;
  401. $fr_info = title_field_replacement_info($entity_type);
  402. if ($fr_info) {
  403. foreach ($fr_info as $legacy_field => $info) {
  404. if (title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
  405. $function = 'title_field_sync_' . ($set ? 'set' : 'get');
  406. $function($entity_type, $entity, $legacy_field, $info, $langcode);
  407. }
  408. }
  409. }
  410. }
  411. /**
  412. * Synchronize a single legacy field with its regular field value.
  413. *
  414. * @param $entity_type
  415. * The name of the entity type.
  416. * @param $entity
  417. * The entity to work with.
  418. * @param $legacy_field
  419. * The name of the legacy field to be replaced.
  420. * @param $field_name
  421. * The regular field to use as source value.
  422. * @param $info
  423. * Field replacement information for the given entity.
  424. * @param $langcode
  425. * The field language to use for the source value.
  426. */
  427. function title_field_sync_get($entity_type, $entity, $legacy_field, $info, $langcode = NULL) {
  428. if (property_exists($entity, $legacy_field)) {
  429. // Save the legacy field value to LEGACY_FIELD_NAME_original.
  430. $entity->{$legacy_field . '_original'} = $entity->{$legacy_field};
  431. // Find out the actual language to use (field might be untranslatable).
  432. $langcode = field_language($entity_type, $entity, $info['field']['field_name'], $langcode);
  433. $values = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
  434. foreach ($values as $name => $value) {
  435. $entity->{$name} = $value;
  436. }
  437. }
  438. }
  439. /**
  440. * Synchronize a single regular field from its legacy field value.
  441. *
  442. * @param $entity_type
  443. * The name of the entity type.
  444. * @param $entity
  445. * The entity to work with.
  446. * @param $legacy_field
  447. * The name of the legacy field to be replaced.
  448. * @param $field_name
  449. * The regular field to use as source value.
  450. * @param $info
  451. * Field replacement information for the given entity.
  452. * @param $langcode
  453. * The field language to use for the target value.
  454. */
  455. function title_field_sync_set($entity_type, $entity, $legacy_field, $info, $langcode) {
  456. if (property_exists($entity, $legacy_field)) {
  457. // Find out the actual language to use (field might be untranslatable).
  458. $field = field_info_field($info['field']['field_name']);
  459. $langcode = field_is_translatable($entity_type, $field) ? $langcode : LANGUAGE_NONE;
  460. $info['callbacks']['sync_set']($entity_type, $entity, $legacy_field, $info, $langcode);
  461. }
  462. }
  463. /**
  464. * Returns and optionally stores the active language.
  465. *
  466. * @param string $langcode
  467. * (optional) The active language to be set. If none is provided the active
  468. * language is just returned.
  469. *
  470. * @return string
  471. * The active language code. Defaults to the current content language.
  472. */
  473. function title_active_language($langcode = NULL) {
  474. static $drupal_static_fast;
  475. if (!isset($drupal_static_fast)) {
  476. $drupal_static_fast['active_language'] = &drupal_static(__FUNCTION__);
  477. }
  478. $active_langcode = &$drupal_static_fast['active_language'];
  479. if (isset($langcode)) {
  480. $active_langcode = $langcode;
  481. }
  482. if (empty($active_langcode)) {
  483. $active_langcode = $GLOBALS['language_content']->language;
  484. }
  485. return $active_langcode;
  486. }
  487. /**
  488. * Provide the original entity language.
  489. *
  490. * If a language property is defined for the current entity we synchronize the
  491. * field value using the entity language, otherwise we fall back to
  492. * LANGUAGE_NONE.
  493. *
  494. * @param $entity_type
  495. * @param $entity
  496. *
  497. * @return
  498. * A language code
  499. */
  500. function title_entity_language($entity_type, $entity) {
  501. if (module_exists('entity_translation') && entity_translation_enabled($entity_type)) {
  502. $handler = entity_translation_get_handler($entity_type, $entity, TRUE);
  503. $langcode = $handler->getLanguage();
  504. }
  505. else {
  506. $langcode = entity_language($entity_type, $entity);
  507. }
  508. return !empty($langcode) ? $langcode : LANGUAGE_NONE;
  509. }
  510. /**
  511. * Implements hook_field_attach_form().
  512. *
  513. * Hide legacy field widgets on the assumption that this is always called on
  514. * fieldable entity forms.
  515. */
  516. function title_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  517. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  518. $fr_info = title_field_replacement_info($entity_type);
  519. if (!empty($fr_info)) {
  520. foreach ($fr_info as $legacy_field => $info) {
  521. if (isset($form[$legacy_field]) && title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
  522. // Inherit the access from the title widget, so that other modules
  523. // restricting access to it keep working.
  524. if (isset($form[$legacy_field]['#access'])) {
  525. $form[$info['field']['field_name']]['#access'] = $form[$legacy_field]['#access'];
  526. }
  527. // Restrict access to the legacy field form element and mark it as
  528. // replaced.
  529. $form[$legacy_field]['#access'] = FALSE;
  530. $form[$legacy_field]['#field_replacement'] = TRUE;
  531. }
  532. }
  533. }
  534. }
  535. /**
  536. * Implements hook_field_attach_submit().
  537. *
  538. * Synchronize submitted field values into the corresponding legacy fields.
  539. */
  540. function title_field_attach_submit($entity_type, $entity, $form, &$form_state) {
  541. $fr_info = title_field_replacement_info($entity_type);
  542. if (!empty($fr_info)) {
  543. // We copy (rather than reference) the values from $form_state because the
  544. // subsequent call to drupal_array_get_nested_value() is destructive and
  545. // will affect other hooks relying on data in $form_state. At the end, we
  546. // copy any modified value back into the $form_state array using
  547. // drupal_array_set_nested_value().
  548. $values = $form_state['values'];
  549. $values = drupal_array_get_nested_value($values, $form['#parents']);
  550. $langcode = entity_language($entity_type, $entity);
  551. foreach ($fr_info as $legacy_field => $info) {
  552. if (!empty($form[$legacy_field]['#field_replacement'])) {
  553. $field_name = $info['field']['field_name'];
  554. // Give a chance to operate on submitted values either.
  555. if (!empty($info['callbacks']['submit'])) {
  556. $info['callbacks']['submit']($entity_type, $entity, $legacy_field, $info, $langcode, $values);
  557. }
  558. drupal_static_reset('field_language');
  559. title_field_sync_get($entity_type, $entity, $legacy_field, $info, $langcode);
  560. }
  561. }
  562. drupal_array_set_nested_value($form_state['values'], $form['#parents'], $values);
  563. }
  564. }
  565. /**
  566. * Implements of hook_menu().
  567. */
  568. function title_menu() {
  569. $items = array();
  570. foreach (entity_get_info() as $entity_type => $entity_info) {
  571. if (!empty($entity_info['field replacement'])) {
  572. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  573. // Blindly taken from field_ui_menu().
  574. if (isset($bundle_info['admin'])) {
  575. $path = $bundle_info['admin']['path'];
  576. if (isset($bundle_info['admin']['bundle argument'])) {
  577. $bundle_arg = $bundle_info['admin']['bundle argument'];
  578. }
  579. else {
  580. $bundle_arg = $bundle_name;
  581. }
  582. $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
  583. $access += array(
  584. 'access callback' => 'user_access',
  585. 'access arguments' => array('administer site configuration'),
  586. );
  587. $path = "$path/fields/replace/%";
  588. $field_arg = count(explode('/', $path)) - 1;
  589. $items[$path] = array(
  590. 'load arguments' => array(),
  591. 'title' => 'Replace fields',
  592. 'page callback' => 'drupal_get_form',
  593. 'page arguments' => array('title_field_replacement_form', $entity_type, $bundle_arg, $field_arg),
  594. 'file' => 'title.admin.inc',
  595. ) + $access;
  596. }
  597. }
  598. }
  599. }
  600. $items['admin/config/content/title'] = array(
  601. 'title' => 'Title settings',
  602. 'description' => 'Settings for the Title module.',
  603. 'page callback' => 'drupal_get_form',
  604. 'page arguments' => array('title_admin_settings_form'),
  605. 'access arguments' => array('administer site configuration'),
  606. 'file' => 'title.admin.inc',
  607. );
  608. return $items;
  609. }
  610. /**
  611. * Implements hook help.
  612. */
  613. function title_help($path, $arg) {
  614. switch ($path) {
  615. case 'admin/config/content/title':
  616. 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 possibile to configure them so that the selected fields are created automatically when a new bundle is created.') . '</p>';
  617. }
  618. }
  619. /**
  620. * Implements hook_field_extra_fields_alter().
  621. */
  622. function title_field_extra_fields_alter(&$info) {
  623. $entity_info = entity_get_info();
  624. foreach ($info as $entity_type => $bundles) {
  625. foreach ($bundles as $bundle_name => $bundle) {
  626. if (!empty($entity_info[$entity_type]['field replacement'])) {
  627. foreach ($entity_info[$entity_type]['field replacement'] as $field_name => $field_replacement_info) {
  628. if (title_field_replacement_enabled($entity_type, $bundle_name, $field_name)) {
  629. // Remove the replaced legacy field.
  630. unset($info[$entity_type][$bundle_name]['form'][$field_name], $info[$entity_type][$bundle_name]['display'][$field_name]);
  631. }
  632. }
  633. }
  634. }
  635. }
  636. }
  637. /**
  638. * Implements hook_form_FORM_ID_alter().
  639. */
  640. function title_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
  641. module_load_include('inc', 'title', 'title.admin');
  642. title_form_field_ui_overview($form, $form_state);
  643. }
  644. /**
  645. * Implements hook_tokens_alter().
  646. *
  647. * Make sure tokens are properly translated.
  648. */
  649. function title_tokens_alter(array &$replacements, array $context) {
  650. $mapping = &drupal_static(__FUNCTION__);
  651. if (empty($mapping)) {
  652. foreach (entity_get_info() as $entity_type => $info) {
  653. if (!empty($info['token type'])) {
  654. $mapping[$info['token type']] = $entity_type;
  655. }
  656. }
  657. }
  658. if (isset($mapping[$context['type']])) {
  659. $entity_type = $mapping[$context['type']];
  660. $fr_info = title_field_replacement_info($entity_type);
  661. if ($fr_info && !empty($context['data'][$context['type']])) {
  662. $entity = $context['data'][$context['type']];
  663. list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  664. $options = $context['options'];
  665. // Since Title tokens are mostly used in storage contexts we default to
  666. // the current working language, that is the entity language. Modules
  667. // using Title tokens in display contexts need to specify the current
  668. // display language.
  669. $langcode = isset($options['language']) ? $options['language']->language : entity_language($entity_type, $entity);
  670. if ($fr_info) {
  671. foreach ($fr_info as $legacy_field => $info) {
  672. if (title_field_replacement_enabled($entity_type, $bundle, $legacy_field)) {
  673. if (isset($context['tokens'][$legacy_field])) {
  674. $langcode = field_language($entity_type, $entity, $info['field']['field_name'], $langcode);
  675. $values = $info['callbacks']['sync_get']($entity_type, $entity, $legacy_field, $info, $langcode);
  676. $item = $values[$legacy_field];
  677. if (!empty($item)) {
  678. if (is_array($item)) {
  679. $item = reset($item);
  680. }
  681. $replacements[$context['tokens'][$legacy_field]] = $item;
  682. }
  683. }
  684. }
  685. }
  686. }
  687. }
  688. }
  689. }
  690. /**
  691. * Implements hook_form_FORM_ID_alter().
  692. */
  693. function title_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  694. $instance = $form['#instance'];
  695. $entity_type = $instance['entity_type'];
  696. if (title_field_replacement_is_label($entity_type, $instance['field_name'])) {
  697. $info = entity_get_info($entity_type);
  698. $form['instance']['settings']['hide_label'] = _title_hide_label_widget($instance['settings'], $info['label']);
  699. }
  700. }
  701. /**
  702. * Returns the hide label form widget.
  703. */
  704. function _title_hide_label_widget($default, $entity_label) {
  705. return array(
  706. '#type' => 'checkboxes',
  707. '#title' => t('Label replacement'),
  708. '#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)),
  709. '#default_value' => !empty($default['hide_label']) ? $default['hide_label'] : array(),
  710. '#options' => array(
  711. 'page' => t('Hide page title'),
  712. 'entity' => t('Hide label in %entity_label listings', array('%entity_label' => drupal_strtolower($entity_label))),
  713. ),
  714. );
  715. }
  716. /**
  717. * Checks whether the given field name is a replaced entity label.
  718. *
  719. * @param $entity_type
  720. * The name of the entity type.
  721. * @param $field_name
  722. * The replacing field name.
  723. *
  724. * @return
  725. * TRUE id the give field is replacing the entity label, FALSE otherwise.
  726. */
  727. function title_field_replacement_is_label($entity_type, $field_name) {
  728. $label = FALSE;
  729. $legacy_field = title_field_replacement_get_legacy_field($entity_type, $field_name);
  730. if ($legacy_field) {
  731. $info = entity_get_info($entity_type);
  732. $label = $legacy_field == $info['entity keys']['label'];
  733. }
  734. return $label;
  735. }
  736. /**
  737. * Returns the legacy field replaced by the given field name.
  738. *
  739. * @param $entity_type
  740. * The name of the entity type.
  741. * @param $field_name
  742. * The replacing field name.
  743. *
  744. * @return
  745. * The replaced legacy field name or FALSE if none available.
  746. */
  747. function title_field_replacement_get_legacy_field($entity_type, $field_name) {
  748. $result = FALSE;
  749. $fr_info = title_field_replacement_info($entity_type);
  750. if ($fr_info) {
  751. foreach ($fr_info as $legacy_field => $info) {
  752. if ($info['field']['field_name'] == $field_name) {
  753. $result = $legacy_field;
  754. break;
  755. }
  756. }
  757. }
  758. return $result;
  759. }
  760. /**
  761. * Returns the field instance replacing the given entity type's label.
  762. *
  763. * @param $entity_type
  764. * The name of the entity type.
  765. * @param $bundle
  766. * The name of the bundle the instance is attached to.
  767. *
  768. * @return
  769. * The field instance replacing the label or FALSE if none available.
  770. */
  771. function title_field_replacement_get_label_field($entity_type, $bundle) {
  772. $instance = FALSE;
  773. $info = entity_get_info($entity_type);
  774. if (!empty($info['field replacement'])) {
  775. $fr_info = $info['field replacement'];
  776. $legacy_field = $info['entity keys']['label'];
  777. if (!empty($fr_info[$legacy_field]['field'])) {
  778. $instance = field_info_instance($entity_type, $fr_info[$legacy_field]['field']['field_name'], $bundle);
  779. }
  780. }
  781. return $instance;
  782. }
  783. /**
  784. * Hides the label from the given variables.
  785. *
  786. * @param $entity_type
  787. * The name of the entity type.
  788. * @param $entity
  789. * The entity to work with.
  790. * @param $vaiables
  791. * A reference to the variables array related to the template being processed.
  792. * @param $page
  793. * (optional) The current render phase: page or entity. Defaults to entity.
  794. */
  795. function title_field_replacement_hide_label($entity_type, $entity, &$variables, $page = FALSE) {
  796. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  797. $instance = title_field_replacement_get_label_field($entity_type, $bundle);
  798. $settings_key = $page ? 'page' : 'entity';
  799. if (!empty($instance['settings']['hide_label'][$settings_key])) {
  800. // If no key is passed default to the label one.
  801. if ($page) {
  802. $key = 'title';
  803. }
  804. else {
  805. $info = entity_get_info($entity_type);
  806. $key = $info['field replacement'][$info['entity keys']['label']]['preprocess_key'];
  807. }
  808. // We cannot simply unset the variable value since this may cause templates
  809. // to throw notices.
  810. $variables[$key] = FALSE;
  811. }
  812. }
  813. /**
  814. * Implements hook_views_api().
  815. */
  816. function title_views_api() {
  817. return array(
  818. 'api' => 3,
  819. 'path' => drupal_get_path('module', 'title') . '/views',
  820. );
  821. }
  822. /**
  823. * Implements hook_field_attach_create_bundle().
  824. *
  825. * Automatically attach the replacement field to the new bundle.
  826. */
  827. function title_field_attach_create_bundle($entity_type, $bundle) {
  828. $entity_info = entity_get_info($entity_type);
  829. if (empty($entity_info['field replacement'])) {
  830. return;
  831. }
  832. $options = variable_get('title_' . $entity_type, array());
  833. foreach (array_keys($entity_info['field replacement']) as $legacy_field) {
  834. if (empty($options['auto_attach'][$legacy_field])) {
  835. continue;
  836. }
  837. // Do not continue if the replacement field already exists.
  838. $field_name = $entity_info['field replacement'][$legacy_field]['field']['field_name'];
  839. if (field_info_instance($entity_type, $field_name, $bundle)) {
  840. continue;
  841. }
  842. title_field_replacement_toggle($entity_type, $bundle, $legacy_field);
  843. $instance = field_info_instance($entity_type, $field_name, $bundle);
  844. if ($instance) {
  845. $params = array(
  846. '@entity_label' => drupal_strtolower($entity_info['label']),
  847. '%field_name' => $instance['label'],
  848. );
  849. drupal_set_message(t('The @entity_label %field_name field was automatically replaced.', $params));
  850. }
  851. }
  852. }