metatag.module 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. <?php
  2. /**
  3. * @file
  4. * Primary hook implementations for Metatag.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function metatag_help($path, $arg) {
  10. if ($path == 'admin/config/search/metatags') {
  11. return '<p>' . t('To view a summary of the default meta tags and the inheritance, click on a meta tag type.') . '</p>';
  12. }
  13. elseif ($path == 'admin/help#metatag') {
  14. return '<p>' . t('The Metatag module provides a options to let each page have customized meta data added to the "meta" tags in the HEAD section of the document.') . '</p>';
  15. }
  16. elseif ($path == 'admin/config/search/metatags/bulk-revert') {
  17. return '<p>' . t('This form <strong>will wipe out</strong> all custom meta tags for the selected entities, reverting them to the default configuration assigned at the <a href="@url">Defaults tab</a>. For example, if the meta tags are changed for an article they will be removed if the "Node: Article" checkbox is selected.', array('@url' => url('admin/config/search/metatags'))) . '</p>';
  18. }
  19. }
  20. /**
  21. * Implements hook_theme().
  22. */
  23. function metatag_theme() {
  24. $info['metatag'] = array(
  25. 'render element' => 'element',
  26. 'file' => 'metatag.theme.inc',
  27. );
  28. $info['metatag_http_equiv'] = array(
  29. 'render element' => 'element',
  30. 'file' => 'metatag.theme.inc',
  31. );
  32. $info['metatag_link_rel'] = array(
  33. 'render element' => 'element',
  34. 'file' => 'metatag.theme.inc',
  35. );
  36. $info['metatag_link_rev'] = array(
  37. 'render element' => 'element',
  38. 'file' => 'metatag.theme.inc',
  39. );
  40. return $info;
  41. }
  42. /**
  43. * Implements hook_ctools_plugin_api().
  44. */
  45. function metatag_ctools_plugin_api($owner, $api) {
  46. if ($owner == 'metatag' && $api == 'metatag') {
  47. return array('version' => 1);
  48. }
  49. }
  50. /**
  51. * Implements hook_hook_info().
  52. */
  53. function metatag_hook_info() {
  54. $hooks = array(
  55. 'metatag_config_default',
  56. 'metatag_config_default_alter',
  57. 'metatag_config_delete',
  58. 'metatag_config_insert',
  59. 'metatag_config_instance_info',
  60. 'metatag_config_instance_info_alter',
  61. 'metatag_config_load',
  62. 'metatag_config_load_presave',
  63. 'metatag_config_update',
  64. 'metatag_info',
  65. 'metatag_info_alter',
  66. );
  67. return array_fill_keys($hooks, array('group' => 'metatag'));
  68. }
  69. /**
  70. * Implements hook_permission().
  71. */
  72. function metatag_permission() {
  73. $permissions['administer meta tags'] = array(
  74. 'title' => t('Administer meta tags'),
  75. 'restrict access' => TRUE,
  76. 'description' => t('Control the main settings pages and modify per-object meta tags.'),
  77. );
  78. $permissions['edit meta tags'] = array(
  79. 'title' => t('Edit meta tags'),
  80. 'description' => t('Modify meta tags on individual entity records (nodes, terms, users, etc).'),
  81. );
  82. // Optional extended edit permissions.
  83. if (variable_get('metatag_extended_permissions', FALSE)) {
  84. $permissions['edit meta tags']['description'] .= '<br />' . t('<em>Extended Permissions</em> has been enabled. Roles have the :admin permission will see all meta tags on edit forms, otherwise the permissions below will control which meta tags are available and are needed in addition to <em>Edit meta tags</em>.', array(':admin' => t('Administer meta tags')));
  85. $metatags = metatag_get_info();
  86. foreach ($metatags['tags'] as $metatag_name => $metatag) {
  87. $permissions['edit meta tag: ' . $metatag_name] = array(
  88. 'title' => t('Extended permission: Edit :tag meta tag', array(':tag' => $metatag['label'])),
  89. 'description' => t('Customize the :tag meta tag on individual forms.', array(':tag' => $metatag['label'])),
  90. );
  91. }
  92. }
  93. return $permissions;
  94. }
  95. /**
  96. * Implements hook_menu().
  97. */
  98. function metatag_menu() {
  99. $items['admin/config/search/metatags'] = array(
  100. 'title' => 'Metatag',
  101. 'description' => 'Configure Metatag defaults.',
  102. 'page callback' => 'metatag_config_overview',
  103. 'access arguments' => array('administer meta tags'),
  104. 'file' => 'metatag.admin.inc',
  105. );
  106. $items['admin/config/search/metatags/config'] = array(
  107. 'title' => 'Defaults',
  108. 'type' => MENU_DEFAULT_LOCAL_TASK,
  109. 'weight' => -10,
  110. );
  111. $items['admin/config/search/metatags/config/add'] = array(
  112. 'title' => 'Add a Metatag default',
  113. 'page callback' => 'drupal_get_form',
  114. 'page arguments' => array('metatag_config_add_form'),
  115. 'access arguments' => array('administer meta tags'),
  116. 'file' => 'metatag.admin.inc',
  117. 'type' => MENU_LOCAL_ACTION,
  118. );
  119. $items['admin/config/search/metatags/config/%metatag_config'] = array(
  120. 'title callback' => 'metatag_config_title',
  121. 'title arguments' => array(5),
  122. 'page callback' => 'drupal_get_form',
  123. 'page arguments' => array('metatag_config_edit_form', 5),
  124. 'access arguments' => array('administer meta tags'),
  125. 'file' => 'metatag.admin.inc',
  126. );
  127. $items['admin/config/search/metatags/config/%metatag_config/edit'] = array(
  128. 'title' => 'Edit',
  129. 'type' => MENU_DEFAULT_LOCAL_TASK,
  130. 'weight' => -10,
  131. );
  132. $items['admin/config/search/metatags/config/%metatag_config/enable'] = array(
  133. 'title' => 'Enable',
  134. 'page callback' => 'metatag_config_enable',
  135. 'page arguments' => array(5),
  136. 'access callback' => 'metatag_config_access',
  137. 'access arguments' => array('enable', 5),
  138. 'file' => 'metatag.admin.inc',
  139. );
  140. $items['admin/config/search/metatags/config/%metatag_config/disable'] = array(
  141. 'title' => 'Disable',
  142. 'page callback' => 'metatag_config_disable',
  143. 'page arguments' => array(5),
  144. 'access callback' => 'metatag_config_access',
  145. 'access arguments' => array('disable', 5),
  146. 'file' => 'metatag.admin.inc',
  147. );
  148. $items['admin/config/search/metatags/config/%metatag_config/revert'] = array(
  149. 'title' => 'Revert',
  150. 'page callback' => 'drupal_get_form',
  151. 'page arguments' => array('metatag_config_delete_form', 5),
  152. 'access callback' => 'metatag_config_access',
  153. 'access arguments' => array('revert', 5),
  154. 'file' => 'metatag.admin.inc',
  155. 'type' => MENU_LOCAL_TASK,
  156. );
  157. $items['admin/config/search/metatags/config/%metatag_config/delete'] = array(
  158. 'title' => 'Delete',
  159. 'page callback' => 'drupal_get_form',
  160. 'page arguments' => array('metatag_config_delete_form', 5),
  161. 'access callback' => 'metatag_config_access',
  162. 'access arguments' => array('delete', 5),
  163. 'file' => 'metatag.admin.inc',
  164. );
  165. $items['admin/config/search/metatags/config/%metatag_config/export'] = array(
  166. 'title' => 'Export',
  167. 'page callback' => 'metatag_config_export_form',
  168. 'page arguments' => array(5),
  169. 'access arguments' => array('administer meta tags'),
  170. 'file' => 'metatag.admin.inc',
  171. 'type' => MENU_LOCAL_TASK,
  172. 'weight' => 10,
  173. );
  174. $items['admin/config/search/metatags/bulk-revert'] = array(
  175. 'title' => 'Bulk revert',
  176. 'page callback' => 'drupal_get_form',
  177. 'page arguments' => array('metatag_bulk_revert_form'),
  178. 'access arguments' => array('administer meta tags'),
  179. 'type' => MENU_LOCAL_TASK,
  180. 'weight' => 30,
  181. 'file' => 'metatag.admin.inc',
  182. );
  183. return $items;
  184. }
  185. /**
  186. * Implements hook_flush_caches().
  187. */
  188. function metatag_flush_caches() {
  189. return array('cache_metatag');
  190. }
  191. /**
  192. * Load a metatag configuration record with all the defaults merged in.
  193. *
  194. * For example, given the configuration instance 'node:article', this function
  195. * will load the configuration records for 'node:article', then 'node', and
  196. * then finally 'global', with each attempt using an array merge.
  197. *
  198. * The levels of defaults is arranged by splitting the $instance variable by
  199. * the colon character, and always using a 'global' instance at the end.
  200. */
  201. function metatag_config_load_with_defaults($instance, $include_global = TRUE) {
  202. $defaults = &drupal_static(__FUNCTION__, array());
  203. // Statically cache defaults since they can include multiple levels.
  204. $cid = "config:{$instance}" . ($include_global ? ':withglobal' : ':withoutglobal');
  205. if (!isset($defaults[$cid])) {
  206. if ($cache = cache_get($cid, 'cache_metatag')) {
  207. $defaults[$cid] = $cache->data;
  208. }
  209. else {
  210. $defaults[$cid] = array();
  211. $instances = metatag_config_get_parent_instances($instance, $include_global);
  212. $configs = metatag_config_load_multiple($instances);
  213. foreach ($instances as $key) {
  214. // Ignore disabled configurations.
  215. if (!isset($configs[$key]) || !empty($configs[$key]->disabled)) {
  216. continue;
  217. }
  218. // Add config to the defaults array.
  219. $defaults[$cid] += $configs[$key]->config;
  220. }
  221. cache_set($cid, $defaults[$cid], 'cache_metatag');
  222. }
  223. }
  224. return $defaults[$cid];
  225. }
  226. /**
  227. * Load a metatag configuration record.
  228. */
  229. function metatag_config_load($instance) {
  230. $results = metatag_config_load_multiple(array($instance));
  231. return !empty($results[$instance]) ? $results[$instance] : FALSE;
  232. }
  233. /**
  234. * Load multiple metatag configuration records.
  235. */
  236. function metatag_config_load_multiple(array $instances) {
  237. ctools_include('export');
  238. return ctools_export_load_object('metatag_config', 'names', $instances);
  239. }
  240. /**
  241. * Save a metatag configuration record to the database.
  242. */
  243. function metatag_config_save($config) {
  244. $config->is_new = empty($config->cid);
  245. // Allow modules to alter the configuration before it is saved using
  246. // hook_metatag_config_presave().
  247. module_invoke_all('metatag_config_presave', $config);
  248. // Update the i18n string
  249. if (function_exists('i18n_string_update')) {
  250. $instance = $config->instance;
  251. foreach ($config->config as $field => $item) {
  252. $name = "metatag:" . $instance . ":" . $field;
  253. i18n_string_update($name, $item['value']);
  254. }
  255. }
  256. if ($config->is_new) {
  257. drupal_write_record('metatag_config', $config);
  258. // Allow modules to act upon the record insertion using
  259. // hook_metatag_config_insert().
  260. module_invoke_all('metatag_config_insert', $config);
  261. }
  262. else {
  263. drupal_write_record('metatag_config', $config, array('cid'));
  264. // Allow modules to act upon the record update using
  265. // hook_metatag_config_insert().
  266. module_invoke_all('metatag_config_update', $config);
  267. }
  268. unset($config->is_new);
  269. // Clear any caches.
  270. metatag_config_cache_clear();
  271. }
  272. /**
  273. * Delete a metatag configuration record.
  274. */
  275. function metatag_config_delete($instance) {
  276. db_delete('metatag_config')
  277. ->condition('instance', $instance)
  278. ->execute();
  279. // Clear any caches.
  280. metatag_config_cache_clear();
  281. }
  282. /**
  283. * Clear the metatag configuration cache.
  284. */
  285. function metatag_config_cache_clear() {
  286. cache_clear_all('*', 'cache_metatag', TRUE);
  287. drupal_static_reset('metatag_config_load_with_defaults');
  288. drupal_static_reset('metatag_entity_has_metatags');
  289. drupal_static_reset('metatag_entity_supports_metatags');
  290. ctools_include('export');
  291. ctools_export_load_object_reset('metatag_config');
  292. }
  293. /**
  294. * Load an entity's tags.
  295. *
  296. * @param $entity_type
  297. * The entity type to load
  298. * @param $entity_id
  299. * The ID of the entity to load
  300. * @return
  301. * An array of tag data keyed by language.
  302. */
  303. function metatag_metatags_load($entity_type, $entity_id) {
  304. $metatags = metatag_metatags_load_multiple($entity_type, array($entity_id));
  305. return !empty($metatags) ? reset($metatags) : array();
  306. }
  307. /**
  308. * Load tags for multiple entities.
  309. *
  310. * @param $entity_type
  311. * The entity type to load
  312. * @param $entity_ids
  313. * The list of entity IDs
  314. * @return
  315. * An array of tag data, keyed by ID.
  316. */
  317. function metatag_metatags_load_multiple($entity_type, array $entity_ids, array $revision_ids) {
  318. // Double check entity IDs are numeric thanks to Entity API module.
  319. $entity_ids = array_filter($entity_ids, 'is_numeric');
  320. if (empty($entity_ids)) {
  321. return array();
  322. }
  323. // Also need to check if the metatag table exists since this condition could
  324. // fire before the table has been installed yet.
  325. if (!variable_get('metatag_schema_installed', FALSE)) {
  326. if (db_table_exists('metatag')) {
  327. variable_set('metatag_schema_installed', TRUE);
  328. }
  329. else {
  330. watchdog('metatag', 'The system tried to load metatag data before the schema was fully loaded.', array(), WATCHDOG_WARNING);
  331. return array();
  332. }
  333. }
  334. // Get all translations of tag data for this entity.
  335. $result = db_query("SELECT entity_id, data, language FROM {metatag} WHERE (entity_type = :type) AND (entity_id IN (:ids)) AND (revision_id IN (:vids))", array(
  336. ':type' => $entity_type,
  337. ':ids' => $entity_ids,
  338. ':vids' => $revision_ids,
  339. ));
  340. // Marshal it into an array keyed by entity ID. Each value is an array of
  341. // translations keyed by language code.
  342. $metatags = array();
  343. while ($record = $result->fetchObject()) {
  344. $metatags[$record->entity_id][$record->language] = unserialize($record->data);
  345. }
  346. return $metatags;
  347. }
  348. /**
  349. * Save an entity's tags.
  350. *
  351. * @param $entity_type
  352. * The entity type to load
  353. * @param $entity_id
  354. * The entity's ID
  355. * @param $revision_id
  356. * The entity's VID.
  357. * @param $metatags
  358. * All of the tag information
  359. * @param $language
  360. * The language of the translation set
  361. */
  362. function metatag_metatags_save($entity_type, $entity_id, $revision_id, $metatags, $langcode) {
  363. // If no language assigned, use the has-no-language language.
  364. if (empty($langcode)) {
  365. $langcode = LANGUAGE_NONE;
  366. }
  367. // Check that $entity_id is numeric because of Entity API and string IDs.
  368. if (!is_numeric($entity_id)) {
  369. return;
  370. }
  371. // If the vid was not passed in, use the entity_id.
  372. if (empty($revision_id)) {
  373. $revision_id = $entity_id;
  374. }
  375. // Ensure the data saves during node_save().
  376. if (isset($metatags[$langcode])) {
  377. // There are certain occasions when the old data and the new data are
  378. // *both* added to the $metatags array, in this case throw away the language
  379. // data.
  380. $lang_data = $metatags[$langcode];
  381. unset($metatags[$langcode]);
  382. if (empty($metatags)) {
  383. $metatags = $lang_data;
  384. }
  385. }
  386. // Allow other modules to alter the meta tags prior to saving using
  387. // hook_metatag_presave().
  388. foreach (module_implements('metatag_presave') as $module) {
  389. $function = "{$module}_metatag_presave";
  390. $function($metatags, $entity_type, $entity_id, $revision_id, $langcode);
  391. }
  392. if (empty($metatags)) {
  393. // If the data array is empty, there is no data to actually save, so
  394. // just delete the record from the database.
  395. db_delete('metatag')
  396. ->condition('entity_type', $entity_type)
  397. ->condition('entity_id', $entity_id)
  398. ->condition('language', $langcode)
  399. ->execute();
  400. }
  401. else {
  402. // Otherwise save the data for this entity.
  403. db_merge('metatag')
  404. ->key(array(
  405. 'entity_type' => $entity_type,
  406. 'entity_id' => $entity_id,
  407. 'language' => $langcode,
  408. 'revision_id' => $revision_id,
  409. ))
  410. ->fields(array(
  411. 'data' => serialize($metatags),
  412. ))
  413. ->execute();
  414. }
  415. // Clear cached data.
  416. metatag_metatags_cache_clear($entity_type, $entity_id);
  417. }
  418. /**
  419. * Delete an entity's tags.
  420. *
  421. * @param $entity_type
  422. * The entity type
  423. * @param $entity_id
  424. * The entity's ID
  425. * @param $revision_id
  426. * The entity's VID.
  427. * @param $langcode
  428. * The language ID of the entry to delete. If left blank, all language
  429. * entries for this entity will be deleted.
  430. */
  431. function metatag_metatags_delete($entity_type, $entity_id, $revision_id = NULL, $langcode = NULL) {
  432. return metatag_metatags_delete_multiple($entity_type, array($entity_id), array($revision_id), $langcode);
  433. }
  434. /**
  435. * Delete multiple entities' tags.
  436. *
  437. * @param $entity_type
  438. * The entity type
  439. * @param $entity_ids
  440. * The list of IDs
  441. * @param $revision_id
  442. * An optional list of VIDs, if omitted all revisions will be deleted.
  443. * @param $langcode
  444. * The language ID of the entities to delete. If left blank, all language
  445. * entries for the enities will be deleted.
  446. */
  447. function metatag_metatags_delete_multiple($entity_type, array $entity_ids, array $revision_ids, $langcode = NULL) {
  448. // Double check entity IDs are numeric thanks to Entity API module.
  449. $entity_ids = array_filter($entity_ids, 'is_numeric');
  450. if ($metatags = metatag_metatags_load_multiple($entity_type, $entity_ids, $revision_ids)) {
  451. $transaction = db_transaction();
  452. try {
  453. // Let other modules know about the records being deleted using
  454. // hook_metatag_metatags_delete().
  455. module_invoke_all('metatag_metatags_delete', $entity_type, $entity_ids, $revision_ids, $langcode);
  456. // Set the entity to delete.
  457. $query = db_delete('metatag')
  458. ->condition('entity_type', $entity_type)
  459. ->condition('entity_id', $entity_ids, 'IN');
  460. // Optionally delete a specific revision.
  461. if (!empty($revision_ids)) {
  462. $query->condition('revision_id', $revision_ids, 'IN');
  463. }
  464. // Specify a language if there is one.
  465. if (!empty($langcode)) {
  466. $query->condition('language', $langcode);
  467. }
  468. // Perform the deletion(s).
  469. $query->execute();
  470. // Clear cached data.
  471. metatag_metatags_cache_clear($entity_type, $entity_ids);
  472. }
  473. catch (Exception $e) {
  474. $transaction->rollback();
  475. watchdog_exception('metatag', $e);
  476. throw $e;
  477. }
  478. }
  479. }
  480. function metatag_metatags_cache_clear($entity_type, $entity_id = NULL) {
  481. if (empty($entity_id)) {
  482. cache_clear_all("output:$entity_type", 'cache_metatag', TRUE);
  483. }
  484. else {
  485. $entity_ids = (array) $entity_id;
  486. foreach ($entity_ids as $entity_id) {
  487. cache_clear_all("output:$entity_type:$entity_id", 'cache_metatag', TRUE);
  488. }
  489. }
  490. }
  491. /**
  492. * Implements hook_entity_load().
  493. */
  494. function metatag_entity_load($entities, $entity_type) {
  495. // get the revision_ids
  496. $revision_ids = array();
  497. //since some entities do not have revisions, set the vid to the id
  498. foreach ($entities as $key => $entity) {
  499. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  500. if (!$revision_id) {
  501. $revision_id = $entity_id;
  502. }
  503. $revision_ids[] = $revision_id;
  504. }
  505. // Wrap this in a try-catch block to work around occasions when the schema
  506. // hasn't been updated yet.
  507. try {
  508. if (metatag_entity_supports_metatags($entity_type)) {
  509. $metatags = metatag_metatags_load_multiple($entity_type, array_keys($entities), $revision_ids);
  510. foreach ($entities as $entity_id => $entity) {
  511. $entities[$entity_id]->metatags = isset($metatags[$entity_id]) ? $metatags[$entity_id] : array();
  512. }
  513. }
  514. }
  515. catch (Exception $e) {
  516. watchdog('metatag', 'Error loading meta tag data, do the <a href="@update">database updates</a> need to be run? The error occurred when loading record(s) %ids for the %type entity type. The error message was: %error', array('@update' => base_path() . 'update.php', '%ids' => implode(', ', array_keys($entities)), '%type' => $entity_type, '%error' => $e->getMessage()), WATCHDOG_CRITICAL);
  517. // Don't display the same message twice for Drush.
  518. if (php_sapi_name() != 'cli') {
  519. drupal_set_message(t('Error loading meta tag data, do the <a href="@update">database updates</a> need to be run?', array('@update' => base_path() . 'update.php')), 'error');
  520. }
  521. }
  522. }
  523. /**
  524. * Implements hook_entity_insert().
  525. */
  526. function metatag_entity_insert($entity, $entity_type) {
  527. if (isset($entity->metatags)) {
  528. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  529. // Determine the entity's language.
  530. $langcode = entity_language($entity_type, $entity);
  531. // Unfortunately due to how core works, the normal entity_language()
  532. // function returns 'und' instead of the node's language during node
  533. // creation.
  534. if ((empty($langcode) || $langcode == LANGUAGE_NONE) && !empty($entity->language)) {
  535. $langcode = $entity->language;
  536. }
  537. // If no language was still found, use the 'no language' value.
  538. if (empty($langcode)) {
  539. $langcode = LANGUAGE_NONE;
  540. }
  541. metatag_metatags_save($entity_type, $entity_id, $revision_id, $entity->metatags, $langcode);
  542. }
  543. }
  544. /**
  545. * Implements hook_entity_update().
  546. */
  547. function metatag_entity_update($entity, $entity_type) {
  548. if (!metatag_entity_supports_metatags($entity_type)) {
  549. return;
  550. }
  551. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  552. if (isset($entity->metatags)) {
  553. // Determine the entity's language.
  554. $new_language = metatag_entity_get_language($entity_type, $entity);
  555. // Determine the language for this entity object.
  556. if (isset($entity->original)) {
  557. $old_language = metatag_entity_get_language($entity_type, $entity->original);
  558. // If the language has changed then remove the old one. When a new
  559. // translation is being saved using Entity Translation both values will
  560. // be the same, so this is safe to do.
  561. if ($old_language != $new_language) {
  562. db_delete('metatag')
  563. ->condition('entity_type', $entity_type)
  564. ->condition('entity_id', $entity_id)
  565. ->condition('language', $old_language)
  566. ->execute();
  567. }
  568. }
  569. // Save the record.
  570. metatag_metatags_save($entity_type, $entity_id, $revision_id, $entity->metatags, $new_language);
  571. }
  572. else {
  573. // Still ensure the meta tag output is cached.
  574. metatag_metatags_cache_clear($entity_type, $entity_id);
  575. }
  576. }
  577. /**
  578. * Implements hook_entity_delete().
  579. */
  580. function metatag_entity_delete($entity, $entity_type) {
  581. list($entity_id) = entity_extract_ids($entity_type, $entity);
  582. metatag_metatags_delete($entity_type, $entity_id);
  583. }
  584. /**
  585. * Implements hook_field_attach_delete_revision().
  586. */
  587. function metatag_field_attach_delete_revision($entity_type, $entity) {
  588. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  589. metatag_metatags_delete($entity_type, $entity_id, $revision_id);
  590. }
  591. /**
  592. * Implements hook_taxonomy_term_view_alter().
  593. */
  594. function metatag_taxonomy_term_view_alter(&$build, &$entity_type) {
  595. // This is only needed if hook_entity_view has not been added to core.
  596. // @see http://drupal.org/node/1067120
  597. if (isset($build['#term']) && !function_exists('taxonomy_term_view_multiple')) {
  598. $entity = taxonomy_term_load($build['#term']->tid);
  599. metatag_entity_view($entity, $entity_type, 'full', NULL, TRUE);
  600. }
  601. }
  602. /**
  603. * Implements hook_entity_view().
  604. *
  605. * Provides additional argument to allow the display to be forced, to work
  606. * around problems elsewhere in the APIs.
  607. */
  608. function metatag_entity_view($entity, $entity_type, $view_mode, $langcode, $force = FALSE) {
  609. // Only run this function once per page load.
  610. static $i_will_say_this_only_once = FALSE;
  611. // Only proceed if this entity object is the page being viewed.
  612. if (_metatag_entity_is_page($entity_type, $entity)) {
  613. // Some API calls need to force the data loading.
  614. if (!$force) {
  615. // Only run this function once per page load.
  616. if ($i_will_say_this_only_once) {
  617. return;
  618. }
  619. $i_will_say_this_only_once = TRUE;
  620. }
  621. // If this entity object isn't allowed meta tags, skip it.
  622. if (!metatag_entity_has_metatags($entity_type, $entity)) {
  623. return;
  624. }
  625. // Obbtain some details of the entity that are needed elsewhere.
  626. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  627. $instance = "{$entity_type}:{$bundle}";
  628. // Determine the language this entity actually uses.
  629. $entity_language = metatag_entity_get_language($entity_type, $entity);
  630. // The requested language is different to the entity's language, look for
  631. // a language elsewhere.
  632. if ($entity_language != $langcode) {
  633. // No language was defined for the entity.
  634. if ($entity_language == LANGUAGE_NONE) {
  635. $langcode = LANGUAGE_NONE;
  636. }
  637. else {
  638. $enabled_languages = field_content_languages();
  639. foreach (field_language($entity_type, $entity) as $field => $lang) {
  640. // Only accept actual language values that are properly enabled.
  641. if ($lang != LANGUAGE_NONE && in_array($lang, $enabled_languages)) {
  642. $langcode = $lang;
  643. }
  644. }
  645. }
  646. }
  647. // All applicable pieces for this current page.
  648. $cid_parts = array(
  649. 'entity_type' => $entity_type,
  650. 'bundle' => $bundle,
  651. 'entity_id' => $entity_id,
  652. 'view_mode' => $view_mode,
  653. 'langcode' => $langcode,
  654. 'url' => $GLOBALS['base_url'] . base_path() . current_path(),
  655. );
  656. // Allow each page in a sequence to have different values.
  657. if (isset($_GET['page'])) {
  658. $cid_parts['page'] = $_GET['page'];
  659. }
  660. // Allow other modules to alter the page parts using
  661. // hook_metatag_page_cache_cid_parts_alter().
  662. drupal_alter('metatag_page_cache_cid_parts', $cid_parts);
  663. $cid = "output:{$entity_type}:{$entity_id}:{$revision_id}:{$langcode}:" . hash('sha256', serialize($cid_parts));
  664. if ($cache = cache_get($cid, 'cache_metatag')) {
  665. $output = $cache->data;
  666. }
  667. else {
  668. // Separate the meta tags.
  669. $metatags = isset($entity->metatags) ? $entity->metatags : array();
  670. // Build options for meta tag rendering.
  671. $options = array(
  672. 'entity' => $entity,
  673. 'entity_type' => $entity_type,
  674. 'view_mode' => $view_mode,
  675. );
  676. // Ensure we actually pass a language object rather than language code.
  677. $languages = language_list();
  678. if (isset($languages[$langcode])) {
  679. $options['language'] = $languages[$langcode];
  680. }
  681. // Reload the entity object from cache as it may have been altered.
  682. $token_type = token_get_entity_mapping('entity', $entity_type);
  683. $entities = entity_load($entity_type, array($entity_id));
  684. $options['token data'][$token_type] = $entities[$entity_id];
  685. $options['entity'] = $entities[$entity_id];
  686. // Render the metatags and save to the cache.
  687. $output = metatag_metatags_view($instance, $metatags, $options);
  688. cache_set($cid, $output, 'cache_metatag');
  689. }
  690. // We need to register the term's metatags, so we can later fetch them.
  691. // @see metatag_page_build().
  692. metatag_page_set_metatags($instance, $output);
  693. }
  694. }
  695. /**
  696. * Build a renderable array of meta tag output.
  697. *
  698. * @param string $instance
  699. * The configuration instance key of the meta tags to use, e.g.
  700. * "node:article".
  701. * @param array $metatags
  702. * An array of meta tag data.
  703. * @param array $options
  704. * (optional) An array of options including the following keys and values:
  705. * - language: A language object.
  706. * - token data: An array of data to pass into token_replace() during
  707. * meta tag value generation.
  708. */
  709. function metatag_metatags_view($instance, array $metatags = array(), array $options = array()) {
  710. $output = array();
  711. // Convert language codes to a language object.
  712. if (isset($options['language']) && is_string($options['language'])) {
  713. $languages = language_list();
  714. $options['language'] = isset($languages[$options['language']]) ? $languages[$options['language']] : NULL;
  715. }
  716. // If there are any tags, determine the translation to display.
  717. if (!empty($metatags)) {
  718. // Get the display language; default to the entity's language.
  719. if (isset($options['language']) && isset($options['language']->language) && isset($metatags[$options['language']->language])) {
  720. $metatags = $metatags[$options['language']->language];
  721. }
  722. // If no language requested, use the no-language value.
  723. elseif (!empty($metatags[LANGUAGE_NONE])) {
  724. $metatags = $metatags[LANGUAGE_NONE];
  725. }
  726. else {
  727. $metatags = array();
  728. }
  729. }
  730. // Add any default tags to the mix.
  731. $metatags += metatag_config_load_with_defaults($instance);
  732. $options['instance'] = $instance;
  733. foreach ($metatags as $metatag => $data) {
  734. if ((!empty($data['value']) || (isset($data['value']) && is_numeric($data['value'])))
  735. && $metatag_instance = metatag_get_instance($metatag, $data)) {
  736. $output[$metatag] = $metatag_instance->getElement($options);
  737. }
  738. }
  739. // Allow the output meta tags to be modified using
  740. // hook_metatag_metatags_view_alter().
  741. drupal_alter('metatag_metatags_view', $output, $instance);
  742. return $output;
  743. }
  744. function metatag_metatags_values($instance, array $metatags = array(), array $options = array()) {
  745. $values = array();
  746. // Apply defaults to the data for each language.
  747. foreach ($metatags as $language => $metatag) {
  748. $metatags[$language] += metatag_config_load_with_defaults($instance);
  749. }
  750. // Generate output only if we have a valid language.
  751. if (isset($options['language']) && is_string($options['language']) && isset($metatags[$options['language']])) {
  752. $language = $options['language'];
  753. // Convert language codes to a language object.
  754. $languages = language_list();
  755. $options['language'] = isset($languages[$language]) ? $languages[$language] : NULL;
  756. $options['instance'] = $instance;
  757. // Get output elements.
  758. foreach ($metatags[$language] as $metatag => $data) {
  759. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  760. $values[$metatag] = $metatag_instance->getValue($options);
  761. }
  762. }
  763. }
  764. return array_filter($values, 'drupal_strlen');
  765. }
  766. /**
  767. * Build a FAPI array for editing meta tags.
  768. *
  769. * @param array $form
  770. * The current FAPI array.
  771. * @param string $instance
  772. * The configuration instance key of the metatags to use, e.g. "node:article".
  773. * @param array $metatags
  774. * An array of metatag data.
  775. * @param array $options
  776. * (optional) An array of options including the following keys and values:
  777. * - token types: An array of token types to be passed to theme_token_tree().
  778. */
  779. function metatag_metatags_form(array &$form, $instance, array $metatags = array(), array $options = array()) {
  780. $info = metatag_get_info();
  781. if (empty($info['tags'])) {
  782. return;
  783. }
  784. // Merge in the default options.
  785. $options += array(
  786. 'token types' => array(),
  787. 'defaults' => metatag_config_load_with_defaults($instance),
  788. 'instance' => $instance,
  789. );
  790. $form['metatags'] = array(
  791. '#type' => 'fieldset',
  792. '#title' => t('Meta tags'),
  793. '#multilingual' => TRUE,
  794. '#collapsible' => TRUE,
  795. '#collapsed' => TRUE,
  796. '#tree' => TRUE,
  797. '#access' => user_access('edit meta tags') || user_access('administer meta tags'),
  798. '#weight' => 40,
  799. '#attributes' => array(
  800. 'class' => array('metatags-form'),
  801. ),
  802. '#metatag_defaults' => $options['defaults'],
  803. );
  804. // Only support vertical tabs if there is a vertical tab element.
  805. foreach (element_children($form) as $key) {
  806. if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
  807. $form['metatags']['#group'] = $key;
  808. $form['metatags']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'metatag') . '/metatag.vertical-tabs.js';
  809. break;
  810. }
  811. }
  812. // Merge in the default meta tag configurations.
  813. $metatags += $options['defaults'];
  814. // This will be used later.
  815. $group_metatag_access = array();
  816. // Build the form for each metatag.
  817. foreach ($info['tags'] as $metatag => $metatag_info) {
  818. // @todo Replace context matching with hook_metatag_access().
  819. if (isset($options['context']) && isset($metatag_info['context'])) {
  820. if (!in_array($options['context'], $metatag_info['context'])) {
  821. continue;
  822. }
  823. }
  824. $metatag_instance = metatag_get_instance($metatag, isset($metatags[$metatag]) ? $metatags[$metatag] : array());
  825. if (empty($metatag_instance)) {
  826. continue;
  827. }
  828. // Get the form element from the meta tag class.
  829. $metatag_form = $metatag_instance->getForm($options);
  830. // Add a default value form element.
  831. if (isset($options['defaults'][$metatag]['value'])) {
  832. $metatag_form['default'] = array(
  833. '#type' => 'hidden',
  834. '#value' => $options['defaults'][$metatag]['value'],
  835. );
  836. }
  837. // Optional extended edit permissions.
  838. if (variable_get('metatag_extended_permissions', FALSE)) {
  839. $metatag_form['#access'] = user_access('edit meta tag: ' . $metatag) || user_access('administer meta tags');
  840. }
  841. else {
  842. $metatag_form['#access'] = $form['metatags']['#access'];
  843. }
  844. if (!empty($metatag_info['group'])) {
  845. $group_key = $metatag_info['group'];
  846. if (isset($info['groups'][$group_key]['label']) && !isset($form['metatags'][$group_key])) {
  847. $group = $info['groups'][$group_key] + array('form' => array(), 'description' => NULL);
  848. $form['metatags'][$group_key] = $group['form'] + array(
  849. '#type' => 'fieldset',
  850. '#title' => check_plain($group['label']),
  851. '#description' => filter_xss($group['description']),
  852. '#collapsible' => TRUE,
  853. '#collapsed' => TRUE,
  854. );
  855. }
  856. $form['metatags'][$group_key][$metatag] = $metatag_form + array('#parents' => array('metatags', $metatag));
  857. // Hide the fieldset itself if there is not at least one of the meta tag
  858. // fields visible.
  859. if (variable_get('metatag_extended_permissions', FALSE)) {
  860. $form['metatags'][$group_key]['#access'] = count(element_get_visible_children($form['metatags'][$group_key])) > 0;
  861. }
  862. else {
  863. $form['metatags'][$group_key]['#access'] = $form['metatags']['#access'];
  864. }
  865. // Structure the access parameter into this array, and make use of it
  866. // later when we move on. Besides, this foreach is getting heavy.
  867. $group_metatag_access[$group_key] = $form['metatags'][$group_key]['#access'];
  868. }
  869. else {
  870. $form['metatags'][$metatag] = $metatag_form;
  871. }
  872. }
  873. // Hide the fieldset itself if there is not at least one of the meta tag
  874. // fields visible; only bother checking this if the user had edit access in
  875. // the first place.
  876. if ($form['metatags']['#access'] && variable_get('metatag_extended_permissions', FALSE)) {
  877. $form['metatags']['#access'] = count(element_get_visible_children($form['metatags'])) > 0;
  878. }
  879. // Check the #access of each group. If it passed, we display options for
  880. // tokens. By this we update the #description of each group.
  881. if ($form['metatags']['#access']) {
  882. // Built the token list.
  883. $token_listing_link = theme('token_tree', array('token_types' => $options['token types'], 'dialog' => TRUE));
  884. // Add the token list to the top of the fieldset.
  885. $form['metatags']['#description'] = $token_listing_link;
  886. // Check if each meta tag group is being displayed.
  887. if (!empty($group_metatag_access)) {
  888. foreach ($group_metatag_access as $group_key => $token_access) {
  889. if ($token_access) {
  890. // Update the description.
  891. if (isset($form['metatags'][$group_key]['#description'])) {
  892. $form['metatags'][$group_key]['#description'] .= '<br />';
  893. }
  894. else {
  895. $form['metatags'][$group_key]['#description'] = '';
  896. }
  897. $form['metatags'][$group_key]['#description'] .= $token_listing_link;
  898. }
  899. }
  900. }
  901. }
  902. // Add a submit handler to compare the submitted values against the deafult
  903. // values.
  904. $form += array('#submit' => array());
  905. array_unshift($form['#submit'], 'metatag_metatags_form_submit');
  906. }
  907. /**
  908. * Form submit handler; unset meta tag values that equal their default values.
  909. */
  910. function metatag_metatags_form_submit($form, &$form_state) {
  911. if (!empty($form_state['values']['metatags']) && !empty($form['metatags']['#metatag_defaults'])) {
  912. metatag_filter_values_from_defaults($form_state['values']['metatags'], $form['metatags']['#metatag_defaults']);
  913. }
  914. }
  915. /**
  916. * Implements hook_field_extra_fields().
  917. */
  918. function metatag_field_extra_fields() {
  919. $extra = array();
  920. foreach (entity_get_info() as $entity_type => $entity_info) {
  921. foreach (array_keys($entity_info['bundles']) as $bundle) {
  922. if (metatag_entity_supports_metatags($entity_type, $bundle)) {
  923. $extra[$entity_type][$bundle]['form']['metatags'] = array(
  924. 'label' => t('Meta tags'),
  925. 'description' => t('Meta tag module form elements.'),
  926. 'weight' => 40,
  927. );
  928. }
  929. }
  930. }
  931. return $extra;
  932. }
  933. /**
  934. * Check if an individual entity has meta tags defined, or has defaults.
  935. *
  936. * @param string $entity_type
  937. * An entity type.
  938. * @param object $entity
  939. * An entity object.
  940. *
  941. * @return boolean
  942. * TRUE or FALSE if the entity should have a form for or process meta tags.
  943. */
  944. function metatag_entity_has_metatags($entity_type, $entity) {
  945. // If an entity has custom meta tags assigned, then we should return TRUE.
  946. if (!empty($entity->metatags)) {
  947. return TRUE;
  948. }
  949. // Otherwise, check to see if there exists any enabed configuration for
  950. // either the entity type, or bundle (even if the configuration is empty).
  951. // If no configuration exists, then we should not be displaying the meta tag
  952. // forms or processing meta tags on entity view.
  953. $config_exists = &drupal_static(__FUNCTION__, array());
  954. list( , , $bundle) = entity_extract_ids($entity_type, $entity);
  955. // Do not pretend to have metatags when the bundle does not support them.
  956. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  957. return FALSE;
  958. }
  959. $instance = "{$entity_type}:{$bundle}";
  960. if (!isset($config_exists[$instance])) {
  961. // Check if the intstance or its parents (excluding global) are enabled.
  962. $config_exists[$instance] = metatag_config_is_enabled($instance, TRUE, FALSE);
  963. }
  964. return isset($config_exists[$instance]);
  965. }
  966. /**
  967. * Check whether the requested entity type (and bundle) support metatag.
  968. *
  969. * By default this will be FALSE, support has to be specifically enabled by
  970. * assigning 'metatag' => TRUE within the hook_entity_info() definition for the
  971. * entity.
  972. */
  973. function metatag_entity_supports_metatags($entity_type = NULL, $bundle = NULL) {
  974. $entity_types = &drupal_static(__FUNCTION__);
  975. if (!isset($entity_types)) {
  976. $entity_types = array();
  977. foreach (entity_get_info() as $entity_type_key => $entity_info) {
  978. if (empty($entity_info['metatags'])) {
  979. $entity_types[$entity_type_key] = FALSE;
  980. continue;
  981. }
  982. $entity_types[$entity_type_key] = array();
  983. foreach ($entity_info['bundles'] as $bundle_key => $bundle_info) {
  984. $entity_types[$entity_type_key][$bundle_key] = !isset($bundle_info['metatags']) || !empty($bundle_info['metatags']);
  985. }
  986. }
  987. }
  988. if (isset($entity_type) && isset($bundle)) {
  989. return isset($entity_types[$entity_type][$bundle]) ? $entity_types[$entity_type][$bundle] : FALSE;
  990. }
  991. elseif (isset($entity_type)) {
  992. return isset($entity_types[$entity_type]) ? ($entity_types[$entity_type] !== FALSE) : FALSE;
  993. }
  994. return $entity_types;
  995. }
  996. /**
  997. * Implements hook_entity_info_alter().
  998. *
  999. * Enables Metatag support for the core entities.
  1000. */
  1001. function metatag_entity_info_alter(&$info) {
  1002. $defaults['node'] = array(
  1003. 'path' => 'node/%node',
  1004. 'metatags' => TRUE,
  1005. );
  1006. $defaults['taxonomy_term'] = array(
  1007. 'path' => 'taxonomy/term/%taxonomy_term',
  1008. 'metatags' => TRUE,
  1009. );
  1010. if (module_exists('forum') && ($revision_id = variable_get('forum_nav_vocabulary', 0)) && $vocabulary = taxonomy_vocabulary_load($revision_id)) {
  1011. $defaults['taxonomy_term']['bundles'][$vocabulary->machine_name]['path'] = 'forum/%taxonomy_term';
  1012. }
  1013. $defaults['user'] = array(
  1014. 'path' => 'user/%user',
  1015. 'metatags' => TRUE,
  1016. );
  1017. foreach ($defaults as $key => $entity_defaults) {
  1018. if (isset($info[$key])) {
  1019. $info[$key] = drupal_array_merge_deep($entity_defaults, $info[$key]);
  1020. }
  1021. }
  1022. }
  1023. /**
  1024. * Given a path determine if it is an entity default path.
  1025. *
  1026. * @param $path
  1027. * The internal path. The id of the entity should be in the string as '[id]'.
  1028. * @return
  1029. * An array with the entity type and the loaded entity object.
  1030. */
  1031. function metatag_load_entity_from_path($path) {
  1032. $entity_paths = &drupal_static(__FUNCTION__);
  1033. $result = FALSE;
  1034. if (!isset($entity_paths)) {
  1035. $entity_paths = array();
  1036. foreach (entity_get_info() as $entity_type => $entity_info) {
  1037. if (isset($entity_info['default path'])) {
  1038. $default_path = $entity_info['default path'];
  1039. $default_path = preg_quote($default_path, '/');
  1040. $default_path = str_replace('\[id\]', '(\d+)', $default_path);
  1041. $entity_paths[$entity_type] = $default_path;
  1042. }
  1043. }
  1044. }
  1045. foreach ($entity_paths as $entity_type => $default_path) {
  1046. if (preg_match("/^{$default_path}$/", $path, $matches)) {
  1047. if ($entity = entity_load($entity_type, array($matches[1]))) {
  1048. $result = array('entity_type' => $entity_type, 'entity' => reset($entity));
  1049. }
  1050. break;
  1051. }
  1052. }
  1053. // Allow other modules to customize the data using
  1054. // hook_metatag_load_entity_from_path_alter().
  1055. drupal_alter('metatag_load_entity_from_path', $path, $result);
  1056. return $result;
  1057. }
  1058. /**
  1059. * Add meta tags to be added later with metatag_page_build().
  1060. *
  1061. * @param string $instance
  1062. * The configuration instance key of the meta tags, e.g. "node:article".
  1063. * @param array $metatags
  1064. * An array of meta tags from metatag_metatags_view().
  1065. */
  1066. function metatag_page_set_metatags($instance, $metatags) {
  1067. $page_metatags = &drupal_static(__FUNCTION__, array());
  1068. $page_metatags[$instance] = $metatags;
  1069. }
  1070. /**
  1071. * Retrieve the array of met tags to be added with metatag_page_build().
  1072. */
  1073. function metatag_page_get_metatags() {
  1074. // @todo Add alter to this result?
  1075. return drupal_static('metatag_page_set_metatags', array());
  1076. }
  1077. /**
  1078. * Implements hook_page_build().
  1079. */
  1080. function metatag_page_build(&$page) {
  1081. // Ensure these arrays exist, otherwise several use cases will fail.
  1082. if (!isset($page['content']) || !is_array($page['content'])) {
  1083. $page['content'] = array();
  1084. }
  1085. if (!isset($page['content']['metatags']) || !is_array($page['content']['metatags'])) {
  1086. $page['content']['metatags'] = array();
  1087. }
  1088. // The front page has special consideration.
  1089. $instance = 'global:frontpage';
  1090. if (drupal_is_front_page() && metatag_config_is_enabled($instance)) {
  1091. $instance = 'global:frontpage';
  1092. // These two parts are sufficient given that the homepage is unique.
  1093. $cid_parts = array(
  1094. 'langcode' => $GLOBALS['language_content']->language,
  1095. 'url' => $GLOBALS['base_url'] . base_path() . '<front>',
  1096. );
  1097. // Allow each page in a sequence to have different values.
  1098. if (isset($_GET['page'])) {
  1099. $cid_parts['page'] = $_GET['page'];
  1100. }
  1101. // Allow other modules to customize the data using
  1102. // hook_metatag_page_cache_cid_parts_alter().
  1103. drupal_alter('metatag_page_cache_cid_parts', $cid_parts);
  1104. $cid = "output:{$instance}:" . hash('sha256', serialize($cid_parts));
  1105. if ($cache = cache_get($cid, 'cache_metatag')) {
  1106. $metatags = $cache->data;
  1107. }
  1108. else {
  1109. $metatags = metatag_metatags_view($instance, array());
  1110. cache_set($cid, $metatags, 'cache_metatag');
  1111. }
  1112. $page['content']['metatags'][$instance] = $metatags;
  1113. }
  1114. // Load any meta tags assigned via metatag_page_set_metatags(). Note: this
  1115. // must include the necessary defaults.
  1116. else {
  1117. $page['content']['metatags'] += metatag_page_get_metatags();
  1118. }
  1119. // If no meta tags were loaded, and this is not an admin path, at least load
  1120. // the global defaults. This may be disabled, see README.txt for details.
  1121. if (empty($page['content']['metatags']) && variable_get('metatag_load_all_pages', TRUE) && !path_is_admin(current_path())) {
  1122. $instance = 'global';
  1123. // These two parts are sufficient given that the homepage is unique.
  1124. $cid_parts = array(
  1125. 'langcode' => $GLOBALS['language_content']->language,
  1126. 'url' => $GLOBALS['base_url'] . request_uri(),
  1127. );
  1128. // Allow each page in a sequence to have different values.
  1129. if (isset($_GET['page'])) {
  1130. $cid_parts['page'] = $_GET['page'];
  1131. }
  1132. // Allow other modules to customize the data using
  1133. // hook_metatag_page_cache_cid_parts_alter().
  1134. drupal_alter('metatag_page_cache_cid_parts', $cid_parts);
  1135. $cid = "output:{$instance}:" . hash('sha256', serialize($cid_parts));
  1136. if ($cache = cache_get($cid, 'cache_metatag')) {
  1137. $metatags = $cache->data;
  1138. }
  1139. else {
  1140. $metatags = metatag_metatags_view($instance, array());
  1141. cache_set($cid, $metatags, 'cache_metatag');
  1142. }
  1143. $page['content']['metatags'][$instance] = $metatags;
  1144. }
  1145. }
  1146. /**
  1147. * Returns whether the current page is the page of the passed in entity.
  1148. *
  1149. * @param $entity_type
  1150. * The entity type; e.g. 'node' or 'user'.
  1151. * @param $entity
  1152. * The entity object.
  1153. *
  1154. * @return
  1155. * TRUE if the current page is the page of the specified entity, or FALSE
  1156. * otherwise.
  1157. */
  1158. function _metatag_entity_is_page($entity_type, $entity) {
  1159. $uri = entity_uri($entity_type, $entity);
  1160. return !empty($uri['path']) && current_path() == $uri['path'];
  1161. }
  1162. /**
  1163. * Implements hook_field_attach_rename_bundle().
  1164. */
  1165. function metatag_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  1166. $instance_old = $entity_type . ':' . $bundle_old;
  1167. $instance_new = $entity_type . ':' . $bundle_new;
  1168. if ($config = metatag_config_load($instance_old)) {
  1169. $config->instance = $instance_new;
  1170. metatag_config_save($config);
  1171. metatag_config_delete($instance_old);
  1172. }
  1173. }
  1174. /**
  1175. * Implements hook_field_attach_delete_bundle().
  1176. */
  1177. function metatag_field_attach_delete_bundle($entity_type, $bundle) {
  1178. $instance = $entity_type . ':' . $bundle;
  1179. metatag_config_delete($instance);
  1180. }
  1181. /**
  1182. * Implements hook_field_attach_form().
  1183. */
  1184. function metatag_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  1185. if (!metatag_entity_has_metatags($entity_type, $entity)) {
  1186. return;
  1187. }
  1188. // Entity_Translation will trigger this hook again, skip it.
  1189. if (!empty($form_state['entity_translation']['is_translation'])) {
  1190. return;
  1191. }
  1192. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  1193. $instance = "{$entity_type}:{$bundle}";
  1194. // Grab the meta tags for display in the form if there are any.
  1195. if (!empty($entity->metatags)) {
  1196. // Identify the language to use with this entity.
  1197. $entity_language = metatag_entity_get_language($entity_type, $entity);
  1198. // If this is a new translation using Entity Translation, load the meta
  1199. // tags from the entity's original language.
  1200. if (module_exists('entity_translation') && empty($form['#entity_translation_source_form']) && ($handler = entity_translation_entity_form_get_handler($form, $form_state)) && isset($entity->metatags[$handler->getSourceLanguage()])) {
  1201. $metatags = $entity->metatags[$handler->getSourceLanguage()];
  1202. }
  1203. // Determine from where we should get the tags.
  1204. elseif (isset($entity->metatags[$langcode])) {
  1205. // Set the tags to the translation set matching that of the form.
  1206. $metatags = $entity->metatags[$langcode];
  1207. }
  1208. // There is no translation for this entity's tags in the current
  1209. // language. Instead, display tags in the language of the entity, the
  1210. // source language of translations. The will provide translators with the
  1211. // original text to translate.
  1212. elseif (isset($entity->metatags[$entity_language])) {
  1213. $metatags = $entity->metatags[$entity_language];
  1214. }
  1215. // This is a preview so set the tags to the raw submission data. No
  1216. // language has been set.
  1217. else {
  1218. $metatags = $entity->metatags;
  1219. }
  1220. }
  1221. else {
  1222. $metatags = array();
  1223. }
  1224. $options['token types'] = array(token_get_entity_mapping('entity', $entity_type));
  1225. $options['context'] = $entity_type;
  1226. // Allow hook_metatag_token_types_alter() to modify the defined tokens.
  1227. drupal_alter('metatag_token_types', $options);
  1228. // @todo Remove metatag_form_alter() when http://drupal.org/node/1284642 is fixed in core.
  1229. //metatag_metatags_form($form, $instance, $metatags, $options);
  1230. $form['#metatags'] = array(
  1231. 'instance' => $instance,
  1232. 'metatags' => $metatags,
  1233. 'options' => $options,
  1234. );
  1235. }
  1236. /**
  1237. * Implements hook_form_alter().
  1238. *
  1239. * @todo Remove this when http://drupal.org/node/1284642 is fixed in core.
  1240. */
  1241. function metatag_form_alter(&$form, $form_state, $form_id) {
  1242. if (!empty($form['#metatags']) && !isset($form['metatags'])) {
  1243. extract($form['#metatags']);
  1244. metatag_metatags_form($form, $instance, $metatags, $options);
  1245. unset($form['#metatags']);
  1246. }
  1247. }
  1248. /**
  1249. * Get the meta tag information array of a meta tag.
  1250. *
  1251. * @param $metatag
  1252. * The meta tag name, e.g. description, for which the info shall be returned,
  1253. * or NULL to return an array with info about all meta tags.
  1254. */
  1255. function metatag_get_info($type = NULL, $name = NULL) {
  1256. // Use the advanced drupal_static() pattern, since this is called very often.
  1257. static $drupal_static_fast;
  1258. if (!isset($drupal_static_fast)) {
  1259. $drupal_static_fast['metatag_info'] = &drupal_static(__FUNCTION__);
  1260. }
  1261. $info = &$drupal_static_fast['metatag_info'];
  1262. global $language;
  1263. if (!isset($info)) {
  1264. // hook_metatag_info() includes translated strings, so each language is cached
  1265. // separately.
  1266. $cid = 'info:' . $language->language;
  1267. if ($cache = cache_get($cid, 'cache_metatag')) {
  1268. $info = $cache->data;
  1269. }
  1270. else {
  1271. // Obtain all metatag specs defined in other modules using
  1272. // hook_metatag_info().
  1273. $info = module_invoke_all('metatag_info');
  1274. $info += array('tags' => array(), 'groups' => array());
  1275. // Merge in default values.
  1276. foreach ($info['tags'] as $key => $data) {
  1277. $info['tags'][$key] += array(
  1278. // Merge in default values.
  1279. 'name' => $key,
  1280. 'class' => 'DrupalTextMetaTag',
  1281. );
  1282. }
  1283. // Let other modules alter the entity info using
  1284. // hook_metatag_info_alter().
  1285. drupal_alter('metatag_info', $info);
  1286. cache_set($cid, $info, 'cache_metatag');
  1287. }
  1288. }
  1289. if (isset($type) && isset($name)) {
  1290. return isset($info[$type][$name]) ? $info[$type][$name] : FALSE;
  1291. }
  1292. elseif (isset($type)) {
  1293. return isset($info[$type]) ? $info[$type] : array();
  1294. }
  1295. else {
  1296. return $info;
  1297. }
  1298. }
  1299. function metatag_get_instance($metatag, array $data = array()) {
  1300. $info = metatag_get_info('tags', $metatag);
  1301. if (!empty($info['class']) && class_exists($info['class'])) {
  1302. $class = $info['class'];
  1303. return new $class($info, $data);
  1304. }
  1305. }
  1306. /**
  1307. * Return the string value of a meta tag.
  1308. *
  1309. * @param $metatag
  1310. * The meta tag string.
  1311. * @param $data
  1312. * The array of data for the meta tag class instance.
  1313. * @param $options
  1314. * An optional array of additional options to pass to the getValue() method
  1315. * of the meta tag class instance.
  1316. * - raw: A boolean if TRUE will not perform token replacement.
  1317. *
  1318. * @return
  1319. * A string value.
  1320. */
  1321. function metatag_get_value($metatag, array $data, array $options = array()) {
  1322. $value = '';
  1323. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  1324. $options["instance"] = $metatag;
  1325. $value = $metatag_instance->getValue($options);
  1326. }
  1327. return $value;
  1328. }
  1329. /**
  1330. * Set a variable to be altered in metatag_preprocess_html().
  1331. *
  1332. * @see metatag_get_preprocess_variables()
  1333. * @see metatag_preprocess_html()
  1334. * @see metatag_preprocess_maintenance_page()
  1335. */
  1336. function metatag_set_preprocess_variable($hook, $variable, $value) {
  1337. $variables = &drupal_static(__FUNCTION__, array());
  1338. $variables[$hook][$variable] = $value;
  1339. }
  1340. /**
  1341. * Return an array of variables to be altered in preprocess functions.
  1342. *
  1343. * @see metatag_set_preprocess_variable()
  1344. * @see metatag_preprocess_html()
  1345. * @see metatag_preprocess_maintenance_page()
  1346. */
  1347. function metatag_get_preprocess_variables($hook) {
  1348. $variables = drupal_static('metatag_set_preprocess_variable', array());
  1349. return isset($variables[$hook]) ? $variables[$hook] : array();
  1350. }
  1351. /**
  1352. * Implements hook_preprocess_html().
  1353. */
  1354. function metatag_preprocess_html(&$variables) {
  1355. foreach (metatag_get_preprocess_variables('html') as $variable => $value) {
  1356. $variables[$variable] = $value;
  1357. }
  1358. }
  1359. /**
  1360. * Implements hook_preprocess_maintenance_page().
  1361. */
  1362. function metatag_preprocess_maintenance_page(&$variables) {
  1363. foreach (metatag_get_preprocess_variables('html') as $variable => $value) {
  1364. $variables[$variable] = $value;
  1365. }
  1366. }
  1367. /**
  1368. * Implements hook_html_head_alter().
  1369. */
  1370. function metatag_html_head_alter(&$elements) {
  1371. // Remove duplicate link tags if found.
  1372. $metatags = metatag_get_info('tags');
  1373. foreach (array_keys($metatags) as $name) {
  1374. if (!isset($elements['metatag_' . $name]) || $elements['metatag_' . $name]['#tag'] != 'link') {
  1375. // Only check for link tags added by the metatags module.
  1376. continue;
  1377. }
  1378. foreach (array_keys($elements) as $key) {
  1379. if (strpos($key, 'drupal_add_html_head_link:' . $name . ':') === 0) {
  1380. unset($elements[$key]);
  1381. }
  1382. }
  1383. }
  1384. // Remove the default generator meta tag.
  1385. unset($elements['system_meta_generator']);
  1386. }
  1387. function metatag_metatag_get_form($metatag, array $data = array(), array $options = array()) {
  1388. $instance = metatag_get_instance($metatag, $data);
  1389. return $instance->getForm($options);
  1390. }
  1391. function metatag_config_instance_info($instance = NULL) {
  1392. global $language;
  1393. $info = &drupal_static(__FUNCTION__);
  1394. // hook_metatag_info() includes translated strings, so each language is cached
  1395. // separately.
  1396. $cid = 'metatag:config:instance:info:' . $language->language;
  1397. if (!isset($info)) {
  1398. if ($cache = cache_get($cid, 'cache_metatag')) {
  1399. $info = $cache->data;
  1400. }
  1401. else {
  1402. // Allow modules to act upon the record insertion using
  1403. // hook_metatag_config_instance_info().
  1404. $info = module_invoke_all('metatag_config_instance_info');
  1405. // Allow other modules to customize the data using
  1406. // hook_metatag_config_instance_info_alter().
  1407. drupal_alter('metatag_config_instance_info', $info);
  1408. cache_set($cid, $info, 'cache_metatag');
  1409. }
  1410. }
  1411. if (isset($instance)) {
  1412. return isset($info[$instance]) ? $info[$instance] : FALSE;
  1413. }
  1414. else {
  1415. return $info;
  1416. }
  1417. }
  1418. /**
  1419. * Filter out meta tag values that equal the default values.
  1420. *
  1421. * @todo Use information in $values[$metatag]['default'] rather than a $defaults parameter.
  1422. */
  1423. function metatag_filter_values_from_defaults(array &$values, array $defaults = array()) {
  1424. foreach ($values as $metatag => $data) {
  1425. $default = isset($data['default']) ? $data['default'] : (isset($defaults[$metatag]['value']) ? $defaults[$metatag]['value'] : NULL);
  1426. if (isset($default) && isset($data['value']) && $default === $data['value']) {
  1427. // Meta tag has a default, and it matches user-submitted value.
  1428. unset($values[$metatag]);
  1429. }
  1430. elseif (!isset($default) && (is_string($data['value']) && !drupal_strlen($data['value']) || (is_array($data['value']) && !array_filter($data['value'])))) {
  1431. // Metatag does not have a default, and user did not submit a value.
  1432. unset($values[$metatag]);
  1433. }
  1434. if (isset($values[$metatag]['default'])) {
  1435. // Unset the default hidden value.
  1436. unset($values[$metatag]['default']);
  1437. }
  1438. }
  1439. }
  1440. /**
  1441. * Return all the parents of a given configuration instance.
  1442. *
  1443. * @param $instance
  1444. * A meta tag configuration instance.
  1445. *
  1446. * @return
  1447. * An array of instances starting with the $instance parameter, with the end
  1448. * of the array being the global instance.
  1449. */
  1450. function metatag_config_get_parent_instances($instance, $include_global = TRUE) {
  1451. $parents = array();
  1452. $segments = explode(':', $instance);
  1453. while (count($segments) > 0) {
  1454. $parents[] = implode(':', $segments);
  1455. array_pop($segments);
  1456. }
  1457. if ($include_global && end($parents) !== 'global') {
  1458. $parents[] = 'global';
  1459. }
  1460. reset($parents);
  1461. return $parents;
  1462. }
  1463. /**
  1464. * Get the proper label of a configuration instance.
  1465. *
  1466. * @param $instance
  1467. * A meta tag configuration instance.
  1468. */
  1469. function metatag_config_instance_label($instance) {
  1470. $labels = &drupal_static(__FUNCTION__, array());
  1471. if (!isset($labels[$instance])) {
  1472. $instance_parts = explode(':', $instance);
  1473. $instance_part = array_pop($instance_parts);
  1474. if ($context = metatag_config_instance_info($instance)) {
  1475. $labels[$instance] = $context['label'];
  1476. }
  1477. else {
  1478. $labels[$instance] = t('Unknown (@instance)', array('@instance' => $instance_part));
  1479. }
  1480. // Normally the following would use metatag_config_get_parent_instances()
  1481. // but since we already sliced the instance by separator and removed the
  1482. // last segment, putting the array back together gives us this instance's
  1483. // parent.
  1484. if (!empty($instance_parts)) {
  1485. $labels[$instance] = metatag_config_instance_label(implode(':', $instance_parts)) . ': ' . $labels[$instance];
  1486. }
  1487. }
  1488. return $labels[$instance];
  1489. }
  1490. /**
  1491. * Title callback for meta tag configuration instances.
  1492. */
  1493. function metatag_config_title($config) {
  1494. return metatag_config_instance_label($config->instance);
  1495. }
  1496. /**
  1497. * Access callback for meta tag configuration instances.
  1498. */
  1499. function metatag_config_access($op, $config = NULL) {
  1500. if (!user_access('administer meta tags')) {
  1501. return FALSE;
  1502. }
  1503. if ($op == 'enable') {
  1504. return !empty($config->disabled);
  1505. }
  1506. elseif ($op == 'disable') {
  1507. return empty($config->disabled);
  1508. }
  1509. elseif ($op == 'delete') {
  1510. return ($config->export_type & EXPORT_IN_DATABASE) && !($config->export_type & EXPORT_IN_CODE);
  1511. }
  1512. elseif ($op == 'revert') {
  1513. return ($config->export_type & EXPORT_IN_DATABASE) && ($config->export_type & EXPORT_IN_CODE);
  1514. }
  1515. return FALSE;
  1516. }
  1517. /**
  1518. * Checks if a metatag configuration record is enabled.
  1519. *
  1520. * @param string $instance
  1521. * The configuration instance machine name.
  1522. *
  1523. * @return bool
  1524. * TRUE if the configuration is enabled, or FALSE otherwise.
  1525. */
  1526. function metatag_config_is_enabled($instance, $include_defaults = FALSE, $include_global = TRUE) {
  1527. if ($include_defaults) {
  1528. return (bool) metatag_config_load_with_defaults($instance, $include_global);
  1529. }
  1530. else {
  1531. $config = metatag_config_load($instance);
  1532. return !empty($config) && empty($config->disabled);
  1533. }
  1534. }
  1535. /**
  1536. * Wrapper around entity_language() to use LANGUAGE_NONE if the entity does not
  1537. * have a language assigned.
  1538. *
  1539. * @param $entity_type
  1540. * An entity type's machine name.
  1541. * @param $entity
  1542. * The entity to review;
  1543. *
  1544. * @return
  1545. * A string indicating the language code to be used.
  1546. */
  1547. function metatag_entity_get_language($entity_type, $entity) {
  1548. // Determine the entity's language.
  1549. $langcode = entity_language($entity_type, $entity);
  1550. // If no matching language was found, which will happen for e.g. terms and
  1551. // users, it is normally recommended to use the system default language.
  1552. // However, as the system default language can change, this could potentially
  1553. // cause data loss / confusion problems; as a result use the system "no
  1554. // language" value to avoid any potential problems.
  1555. if (empty($langcode)) {
  1556. $langcode = LANGUAGE_NONE;
  1557. }
  1558. return $langcode;
  1559. }
  1560. /**
  1561. * Implements hook_features_api().
  1562. */
  1563. function metatag_features_api() {
  1564. $components = array(
  1565. 'metatag' => array(
  1566. 'name' => t('Metatag'),
  1567. 'feature_source' => TRUE,
  1568. 'default_hook' => 'metatag_export_default',
  1569. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  1570. 'file' => drupal_get_path('module', 'metatag') . '/metatag.features.inc',
  1571. ),
  1572. );
  1573. return $components;
  1574. }
  1575. /**
  1576. * Implements hook_views_post_render().
  1577. */
  1578. function metatag_views_post_render(&$view, &$output, &$cache) {
  1579. // Build a shortcut to the current display object.
  1580. $display = $view->display[$view->current_display];
  1581. // Only proceed if this view is a full page, don't process block or other
  1582. // Views display objects.
  1583. if ($display->display_plugin == 'page' && isset($display->display_options['path'])) {
  1584. // Check if this is an entity display page, if so trigger
  1585. // hook_entity_view().
  1586. foreach (entity_get_info() as $entity_name => $entity_type) {
  1587. // Entity paths will include an auto-loader that matches the entity's
  1588. // name, thus the path will be 'some/path/%entity_name'.
  1589. if (isset($entity_type['path']) && ($display->display_options['path'] . $entity_name) == $entity_type['path']) {
  1590. // Only proceed if this entity type supports meta tags.
  1591. if (metatag_entity_supports_metatags($entity_name)) {
  1592. // There must be at least one argument and the first argument must be
  1593. // numerical.
  1594. if (!empty($view->args) && is_numeric($view->args[0])) {
  1595. // Only the first argument is used.
  1596. $entities = entity_load($entity_name, array($view->args[0]));
  1597. $entity = array_pop($entities);
  1598. metatag_entity_view($entity, $entity_name, 'full', NULL, TRUE);
  1599. }
  1600. }
  1601. }
  1602. }
  1603. }
  1604. }
  1605. /**
  1606. * Implements hook_ctools_render_alter().
  1607. *
  1608. * Temporary solution to load meta tags on entity pages that are driven by
  1609. * CTools display handlers.
  1610. */
  1611. function metatag_ctools_render_alter(&$info, $page, $context) {
  1612. // Only proceed if this is a full page (don't process individual panes) and
  1613. // there's an 'admin path' for the current task.
  1614. if ($page && !empty($context['task']['admin path'])) {
  1615. // Check if this is an entity display page, if so trigger
  1616. // hook_entity_view().
  1617. foreach (entity_get_info() as $entity_name => $entity_type) {
  1618. // Entity paths will include an auto-loader that matches the entity's
  1619. // name, thus the path will be 'some/path/%entity_name'.
  1620. if (isset($entity_type['path']) && $context['task']['admin path'] == $entity_type['path']) {
  1621. // Only proceed if this entity type supports meta tags.
  1622. if (metatag_entity_supports_metatags($entity_name)) {
  1623. // There must be at least one argument and the first argument must be
  1624. // numerical.
  1625. if (!empty($context['args']) && is_numeric($context['args'][0])) {
  1626. // Only the first argument is used.
  1627. $entities = entity_load($entity_name, array($context['args'][0]));
  1628. $entity = array_pop($entities);
  1629. metatag_entity_view($entity, $entity_name, 'full', NULL, TRUE);
  1630. }
  1631. }
  1632. }
  1633. }
  1634. }
  1635. }
  1636. /**
  1637. * Implements hook_entity_translation_delete().
  1638. *
  1639. * Required for content translations being handled via Entity_Translation to
  1640. * remove the appropriate record when a translation is removed without the
  1641. * corresponding entity record also being removed.
  1642. */
  1643. function metatag_entity_translation_delete($entity_type, $entity, $langcode) {
  1644. // Get the entity's ID.
  1645. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  1646. // Delete the translation.
  1647. metatag_metatags_delete($entity_type, $entity_id, $revision_id, $langcode);
  1648. }
  1649. /**
  1650. * Translates the metatag if i18n_string is enabled.
  1651. * @param $name
  1652. * Array or string concatenated with ':' that contains textgroup and string context
  1653. * @param string $string
  1654. * String in default language or array of strings to be translated
  1655. * @param $options
  1656. * An associative array of additional options. @see i18n_string_translate()
  1657. */
  1658. function metatag_translate($name, $string, $langcode = NULL, $update = FALSE) {
  1659. if (function_exists('i18n_string_translate')) {
  1660. $options = array(
  1661. 'langcode' => $langcode,
  1662. 'update' => $update,
  1663. );
  1664. return i18n_string_translate($name, $string, $options);
  1665. }
  1666. else {
  1667. return $string;
  1668. }
  1669. }