metatag.module 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140
  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. $info['metatag_property'] = array(
  41. 'render element' => 'element',
  42. 'file' => 'metatag.theme.inc',
  43. );
  44. return $info;
  45. }
  46. /**
  47. * Implements hook_ctools_plugin_api().
  48. */
  49. function metatag_ctools_plugin_api($owner, $api) {
  50. if ($owner == 'metatag' && $api == 'metatag') {
  51. return array('version' => 1);
  52. }
  53. }
  54. /**
  55. * Implements hook_ctools_plugin_directory().
  56. */
  57. function metatag_ctools_plugin_directory($owner, $plugin_type) {
  58. if ($owner == 'ctools' && $plugin_type == 'content_types') {
  59. return "plugins/$plugin_type";
  60. }
  61. }
  62. /**
  63. * Implements hook_hook_info().
  64. */
  65. function metatag_hook_info() {
  66. $hooks = array(
  67. 'metatag_config_default',
  68. 'metatag_config_default_alter',
  69. 'metatag_config_delete',
  70. 'metatag_config_insert',
  71. 'metatag_config_instance_info',
  72. 'metatag_config_instance_info_alter',
  73. 'metatag_config_load',
  74. 'metatag_config_load_presave',
  75. 'metatag_config_update',
  76. 'metatag_info',
  77. 'metatag_info_alter',
  78. );
  79. return array_fill_keys($hooks, array('group' => 'metatag'));
  80. }
  81. /**
  82. * Implements hook_permission().
  83. */
  84. function metatag_permission() {
  85. $permissions['administer meta tags'] = array(
  86. 'title' => t('Administer meta tags'),
  87. 'restrict access' => TRUE,
  88. 'description' => t('Control the main settings pages and modify per-object meta tags.'),
  89. );
  90. $permissions['edit meta tags'] = array(
  91. 'title' => t('Edit meta tags'),
  92. 'description' => t('Modify meta tags on individual entity records (nodes, terms, users, etc).'),
  93. );
  94. // Optional extended edit permissions.
  95. if (variable_get('metatag_extended_permissions', FALSE)) {
  96. $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')));
  97. $metatags = metatag_get_info();
  98. foreach ($metatags['tags'] as $metatag_name => $metatag) {
  99. $permissions['edit meta tag: ' . $metatag_name] = array(
  100. 'title' => t('Extended permission: Edit :tag meta tag', array(':tag' => $metatag['label'])),
  101. 'description' => t('Customize the :tag meta tag on individual forms.', array(':tag' => $metatag['label'])),
  102. );
  103. }
  104. }
  105. return $permissions;
  106. }
  107. /**
  108. * Implements hook_menu().
  109. */
  110. function metatag_menu() {
  111. $items['admin/config/search/metatags'] = array(
  112. 'title' => 'Metatag',
  113. 'description' => 'Configure Metatag defaults.',
  114. 'page callback' => 'metatag_config_overview',
  115. 'access arguments' => array('administer meta tags'),
  116. 'file' => 'metatag.admin.inc',
  117. );
  118. $items['admin/config/search/metatags/config'] = array(
  119. 'title' => 'Defaults',
  120. 'type' => MENU_DEFAULT_LOCAL_TASK,
  121. 'weight' => -10,
  122. );
  123. $items['admin/config/search/metatags/config/add'] = array(
  124. 'title' => 'Add default meta tags',
  125. 'page callback' => 'drupal_get_form',
  126. 'page arguments' => array('metatag_config_add_form'),
  127. 'access arguments' => array('administer meta tags'),
  128. 'file' => 'metatag.admin.inc',
  129. 'type' => MENU_LOCAL_ACTION,
  130. );
  131. $items['admin/config/search/metatags/config/%metatag_config'] = array(
  132. 'title callback' => 'metatag_config_title',
  133. 'title arguments' => array(5),
  134. 'page callback' => 'drupal_get_form',
  135. 'page arguments' => array('metatag_config_edit_form', 5),
  136. 'access arguments' => array('administer meta tags'),
  137. 'file' => 'metatag.admin.inc',
  138. );
  139. $items['admin/config/search/metatags/config/%metatag_config/edit'] = array(
  140. 'title' => 'Edit',
  141. 'type' => MENU_DEFAULT_LOCAL_TASK,
  142. 'weight' => -10,
  143. );
  144. $items['admin/config/search/metatags/config/%metatag_config/enable'] = array(
  145. 'title' => 'Enable',
  146. 'page callback' => 'metatag_config_enable',
  147. 'page arguments' => array(5),
  148. 'access callback' => 'metatag_config_access',
  149. 'access arguments' => array('enable', 5),
  150. 'file' => 'metatag.admin.inc',
  151. );
  152. $items['admin/config/search/metatags/config/%metatag_config/disable'] = array(
  153. 'title' => 'Disable',
  154. 'page callback' => 'metatag_config_disable',
  155. 'page arguments' => array(5),
  156. 'access callback' => 'metatag_config_access',
  157. 'access arguments' => array('disable', 5),
  158. 'file' => 'metatag.admin.inc',
  159. );
  160. $items['admin/config/search/metatags/config/%metatag_config/revert'] = array(
  161. 'title' => 'Revert',
  162. 'page callback' => 'drupal_get_form',
  163. 'page arguments' => array('metatag_config_delete_form', 5),
  164. 'access callback' => 'metatag_config_access',
  165. 'access arguments' => array('revert', 5),
  166. 'file' => 'metatag.admin.inc',
  167. 'type' => MENU_LOCAL_TASK,
  168. );
  169. $items['admin/config/search/metatags/config/%metatag_config/delete'] = array(
  170. 'title' => 'Delete',
  171. 'page callback' => 'drupal_get_form',
  172. 'page arguments' => array('metatag_config_delete_form', 5),
  173. 'access callback' => 'metatag_config_access',
  174. 'access arguments' => array('delete', 5),
  175. 'file' => 'metatag.admin.inc',
  176. );
  177. $items['admin/config/search/metatags/config/%metatag_config/export'] = array(
  178. 'title' => 'Export',
  179. 'page callback' => 'metatag_config_export_form',
  180. 'page arguments' => array(5),
  181. 'access arguments' => array('administer meta tags'),
  182. 'file' => 'metatag.admin.inc',
  183. 'type' => MENU_LOCAL_TASK,
  184. 'weight' => 10,
  185. );
  186. $items['admin/config/search/metatags/settings'] = array(
  187. 'title' => 'Settings',
  188. 'page callback' => 'drupal_get_form',
  189. 'page arguments' => array('metatag_admin_settings_form'),
  190. 'access arguments' => array('administer meta tags'),
  191. 'type' => MENU_LOCAL_TASK,
  192. 'weight' => 30,
  193. 'file' => 'metatag.admin.inc',
  194. );
  195. $items['admin/config/search/metatags/bulk-revert'] = array(
  196. 'title' => 'Bulk revert',
  197. 'page callback' => 'drupal_get_form',
  198. 'page arguments' => array('metatag_bulk_revert_form'),
  199. 'access arguments' => array('administer meta tags'),
  200. 'type' => MENU_LOCAL_TASK,
  201. 'weight' => 40,
  202. 'file' => 'metatag.admin.inc',
  203. );
  204. // Optional integration with the i18n_string module for translating the
  205. // configurations. Note: this should also check for 'metatag_i18n_disabled'
  206. // but doing so would require rebuilding the menu cache every time the Metatag
  207. // settings page was saved, which may not be advised. Instead the links to
  208. // these pages on the config pages *do* check the variable, which is close
  209. // enough.
  210. if (module_exists('i18n_string')) {
  211. $items['admin/config/search/metatags/config/%metatag_config/translate'] = array(
  212. 'title' => 'Translate',
  213. 'access arguments' => array('administer meta tags'),
  214. 'page callback' => 'i18n_string_object_translate_page',
  215. 'page arguments' => array('metatag_config', 5),
  216. 'type' => MENU_LOCAL_TASK,
  217. );
  218. $items['admin/config/search/metatags/config/%metatag_config/translate/%i18n_language'] = array(
  219. 'title' => 'Translate',
  220. 'access arguments' => array('administer meta tags'),
  221. 'page callback' => 'i18n_string_object_translate_page',
  222. 'page arguments' => array('metatag_config', 5, 7),
  223. 'type' => MENU_CALLBACK,
  224. );
  225. }
  226. return $items;
  227. }
  228. /**
  229. * Implements hook_flush_caches().
  230. */
  231. function metatag_flush_caches() {
  232. return array('cache_metatag');
  233. }
  234. /**
  235. * Load a metatag configuration record with all the defaults merged in.
  236. *
  237. * For example, given the configuration instance 'node:article', this function
  238. * will load the configuration records for 'node:article', then 'node', and
  239. * then finally 'global', with each attempt using an array merge.
  240. *
  241. * The levels of defaults is arranged by splitting the $instance variable by
  242. * the colon character, and always using a 'global' instance at the end.
  243. */
  244. function metatag_config_load_with_defaults($instance, $include_global = TRUE) {
  245. $defaults = &drupal_static(__FUNCTION__, array());
  246. // Use the current page's locale.
  247. $langcode = $GLOBALS['language_content']->language;
  248. // Statically cache defaults since they can include multiple levels.
  249. $cid = "config:{$instance}:{$langcode}" . ($include_global ? ':withglobal' : ':withoutglobal');
  250. if (!isset($defaults[$cid])) {
  251. if ($cache = metatag_cache_get($cid)) {
  252. $defaults[$cid] = $cache->data;
  253. }
  254. else {
  255. $defaults[$cid] = array();
  256. $instances = metatag_config_get_parent_instances($instance, $include_global);
  257. $configs = metatag_config_load_multiple($instances);
  258. foreach ($instances as $key) {
  259. // Ignore disabled configurations.
  260. if (!isset($configs[$key]) || !empty($configs[$key]->disabled)) {
  261. continue;
  262. }
  263. // Add config to the defaults array.
  264. if (!empty($configs[$key]->config)) {
  265. $defaults[$cid] += $configs[$key]->config;
  266. }
  267. }
  268. metatag_cache_set($cid, $defaults[$cid]);
  269. }
  270. }
  271. return $defaults[$cid];
  272. }
  273. /**
  274. * Load a metatag configuration record.
  275. */
  276. function metatag_config_load($instance) {
  277. $results = metatag_config_load_multiple(array($instance));
  278. return !empty($results[$instance]) ? $results[$instance] : FALSE;
  279. }
  280. /**
  281. * Load multiple metatag configuration records.
  282. */
  283. function metatag_config_load_multiple(array $instances) {
  284. // Load the data.
  285. ctools_include('export');
  286. $configs = ctools_export_load_object('metatag_config', 'names', $instances);
  287. // "Fix" any records that might be using old values. Ideally these will be
  288. // permanently fixed by being re-saved or re-exported.
  289. foreach (metatag_config_get_replacements() as $old_tag => $new_tag) {
  290. foreach ($configs as $instance => $config) {
  291. if (isset($config->config[$old_tag])) {
  292. $config->config[$new_tag] = $config->config[$old_tag];
  293. unset($config->config[$old_tag]);
  294. }
  295. }
  296. }
  297. // Translate the configuration.
  298. if (module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
  299. $options = array();
  300. // By default disable the watchdog logging of translation messages.
  301. $options['watchdog'] = variable_get('metatag_i18n_enable_watchdog', FALSE);
  302. foreach ($configs as $instance => &$config) {
  303. foreach ($config->config as $tag => &$value) {
  304. if (isset($value['value']) && is_string($value['value'])) {
  305. $value['value'] = i18n_string_translate(array(
  306. 'metatag',
  307. 'metatag_config',
  308. $instance,
  309. $tag,
  310. ),
  311. $value['value'],
  312. $options);
  313. }
  314. }
  315. }
  316. }
  317. return $configs;
  318. }
  319. /**
  320. * Identify the meta tags that have been deprecated and replaced by others.
  321. */
  322. function metatag_config_get_replacements() {
  323. $replacements = &drupal_static(__FUNCTION__);
  324. if (!isset($replacements)) {
  325. $replacements = array();
  326. foreach (metatag_get_info('tags') as $tag_name => $tag_info) {
  327. if (!empty($tag_info['replaces'])) {
  328. if (!is_array($tag_info['replaces'])) {
  329. $tag_info['replaces'] = array($tag_info['replaces']);
  330. }
  331. foreach ($tag_info['replaces'] as $replaces) {
  332. $replacements[$replaces] = $tag_name;
  333. }
  334. }
  335. }
  336. }
  337. return $replacements;
  338. }
  339. /**
  340. * Save a metatag configuration record to the database.
  341. */
  342. function metatag_config_save($config) {
  343. $config->is_new = empty($config->cid);
  344. // Allow modules to alter the configuration before it is saved using
  345. // hook_metatag_config_presave().
  346. module_invoke_all('metatag_config_presave', $config);
  347. if ($config->is_new) {
  348. drupal_write_record('metatag_config', $config);
  349. // Allow modules to act upon the record insertion using
  350. // hook_metatag_config_insert().
  351. module_invoke_all('metatag_config_insert', $config);
  352. }
  353. else {
  354. drupal_write_record('metatag_config', $config, array('cid'));
  355. // Allow modules to act upon the record update using
  356. // hook_metatag_config_insert().
  357. module_invoke_all('metatag_config_update', $config);
  358. }
  359. unset($config->is_new);
  360. // Clear any caches.
  361. metatag_config_cache_clear();
  362. }
  363. /**
  364. * Delete a metatag configuration record.
  365. */
  366. function metatag_config_delete($config) {
  367. db_delete('metatag_config')
  368. ->condition('instance', $config->instance)
  369. ->execute();
  370. // Allow modules to act upon the record deletion using
  371. // hook_metatag_config_delete().
  372. module_invoke_all('metatag_config_delete', $config);
  373. // Clear any caches.
  374. metatag_config_cache_clear();
  375. }
  376. /**
  377. * Clear the metatag configuration cache.
  378. */
  379. function metatag_config_cache_clear() {
  380. cache_clear_all('*', 'cache_metatag', TRUE);
  381. drupal_static_reset('metatag_config_load_with_defaults');
  382. drupal_static_reset('metatag_entity_supports_metatags');
  383. drupal_static_reset('metatag_config_instance_info');
  384. drupal_static_reset('metatag_get_info');
  385. ctools_include('export');
  386. ctools_export_load_object_reset('metatag_config');
  387. }
  388. /**
  389. * Load an entity's tags.
  390. *
  391. * @param string $entity_type
  392. * The entity type to load.
  393. * @param int $entity_id
  394. * The ID of the entity to load.
  395. * @param mixed $revision_id
  396. * Optional revision ID to load instead of the entity ID.
  397. *
  398. * @return array
  399. * An array of tag data keyed by language for the entity's current active
  400. * revision.
  401. */
  402. function metatag_metatags_load($entity_type, $entity_id, $revision_id = NULL) {
  403. // A specific revision ID was not requested, so get the active revision ID.
  404. if (is_null($revision_id)) {
  405. // Unfortunately, the only way of getting the active revision ID is to
  406. // first load the entity, and then extract the ID. This is a bit
  407. // performance intensive, but it seems to be the only way of doing it.
  408. $entities = entity_load($entity_type, array($entity_id));
  409. if (!empty($entities[$entity_id])) {
  410. // We only care about the revision_id.
  411. list(, $revision_id,) = entity_extract_ids($entity_type, $entities[$entity_id]);
  412. }
  413. }
  414. // This returns an array nested by the entity ID, the revision ID and the
  415. // langcode.
  416. $metatags = metatag_metatags_load_multiple($entity_type, array($entity_id), array($revision_id));
  417. // Look for records for the requested revision ID.
  418. if (isset($metatags[$entity_id][$revision_id])) {
  419. return $metatags[$entity_id][$revision_id];
  420. }
  421. // Getting to this point means that no meta tags were identified earlier, so
  422. // return an empty array.
  423. return array();
  424. }
  425. /**
  426. * Load tags for multiple entities.
  427. *
  428. * @param string $entity_type
  429. * The entity type to load.
  430. * @param array $entity_ids
  431. * The list of entity IDs.
  432. * @param array $revision_ids
  433. * Optional revision ID to load instead of the entity ID.
  434. *
  435. * @return array
  436. * An array of tag data, keyed by entity ID, revision ID and language.
  437. */
  438. function metatag_metatags_load_multiple($entity_type, array $entity_ids, array $revision_ids = array()) {
  439. // Double check entity IDs are all numeric.
  440. $entity_ids = array_filter($entity_ids, 'is_numeric');
  441. if (empty($entity_ids)) {
  442. return array();
  443. }
  444. // Ensure that the revision IDs are all numeric too.
  445. $revision_ids = array_filter($revision_ids, 'is_numeric');
  446. // Verify that there aren't any empty values copied in from
  447. // metatag_metatags_load(). Note: a zero indicates that the entity record does
  448. // not support revisions, so this is ok to do.
  449. $revision_ids = array_filter($revision_ids);
  450. // Also need to check if the metatag table exists since this condition could
  451. // fire before the table has been installed yet.
  452. if (!variable_get('metatag_schema_installed', FALSE)) {
  453. if (db_table_exists('metatag')) {
  454. variable_set('metatag_schema_installed', TRUE);
  455. }
  456. else {
  457. watchdog('metatag', 'The system tried to load metatag data before the schema was fully loaded.', array(), WATCHDOG_WARNING);
  458. return array();
  459. }
  460. }
  461. // Verify that the metatag.revision_id field has been added to the {metatag}
  462. // table schema.
  463. if (!variable_get('metatag_has_revision_id', FALSE)) {
  464. if (db_field_exists('metatag', 'revision_id')) {
  465. variable_set('metatag_has_revision_id', TRUE);
  466. }
  467. else {
  468. watchdog('metatag', 'The database updates need to be ran.', array(), WATCHDOG_WARNING);
  469. return array();
  470. }
  471. }
  472. // Get all translations of tag data for this entity.
  473. $query = db_select('metatag', 'm')
  474. ->fields('m', array('entity_id', 'revision_id', 'language', 'data'))
  475. ->condition('m.entity_type', $entity_type);
  476. // Filter by revision_ids if they are available. If not, filter by entity_ids.
  477. if (!empty($revision_ids)) {
  478. $query->condition('m.revision_id', $revision_ids, 'IN');
  479. }
  480. else {
  481. $query->condition('m.entity_id', $entity_ids, 'IN');
  482. }
  483. $result = $query->execute();
  484. // Marshal it into an array keyed by entity ID. Each value is an array of
  485. // translations keyed by language code.
  486. $metatags = array();
  487. while ($record = $result->fetchObject()) {
  488. $data = unserialize($record->data);
  489. // "Fix" any records that might be using old values. Ideally these will be
  490. // permanently fixed by being re-saved or re-exported.
  491. foreach (metatag_config_get_replacements() as $old_tag => $new_tag) {
  492. if (isset($data[$old_tag])) {
  493. $data[$new_tag] = $data[$old_tag];
  494. unset($data[$old_tag]);
  495. }
  496. }
  497. $metatags[$record->entity_id][$record->revision_id][$record->language] = $data;
  498. }
  499. return $metatags;
  500. }
  501. /**
  502. * Save an entity's tags.
  503. *
  504. * @param string $entity_type
  505. * The entity type to load.
  506. * @param int $entity_id
  507. * The entity's primary ID.
  508. * @param int $revision_id
  509. * The entity's revision ID.
  510. * @param array $metatags
  511. * All of the tag information, keyed by the language code. Most meta tags use
  512. * the 'value' element, so the structure should look like:
  513. * array(
  514. * LANGUAGE_NONE => array(
  515. * 'title' => array(
  516. * 'value' => "This node's title!",
  517. * ),
  518. * 'og:title' => array(
  519. * 'value' => "This node's title for Open Graph!",
  520. * ),
  521. * 'og:image' => array(
  522. * 'value' => "[node:field_thumbnail]",
  523. * ),
  524. * ),
  525. * );.
  526. * @param string|null $bundle
  527. * The bundle of the entity that is being saved. Optional.
  528. */
  529. function metatag_metatags_save($entity_type, $entity_id, $revision_id, $metatags, $bundle = NULL) {
  530. // Check that $entity_id is numeric because of Entity API and string IDs.
  531. if (!is_numeric($entity_id)) {
  532. return;
  533. }
  534. // Don't do anything if the entity type is not supported.
  535. if (!metatag_entity_supports_metatags($entity_type)) {
  536. return;
  537. }
  538. // Verify the entity bundle is supported, if not available just check the
  539. // entity type.
  540. if (!empty($bundle)) {
  541. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  542. return;
  543. }
  544. }
  545. else {
  546. if (!metatag_entity_supports_metatags($entity_type)) {
  547. return;
  548. }
  549. }
  550. // The revision_id must be a numeric value; some entities use NULL for the
  551. // revision so change that to a zero.
  552. if (is_null($revision_id)) {
  553. $revision_id = 0;
  554. }
  555. // Handle scenarios where the metatags are completely empty, this will have
  556. // the effect of erasing the meta tags for those this entity.
  557. if (empty($metatags)) {
  558. $metatags = array();
  559. // Add an empty array record for each language.
  560. $languages = db_query("SELECT language
  561. FROM {metatag}
  562. WHERE (entity_type = :type)
  563. AND (entity_id = :id)
  564. AND (revision_id = :revision)",
  565. array(
  566. ':type' => $entity_type,
  567. ':id' => $entity_id,
  568. ':revision' => $revision_id,
  569. ))->fetchCol();
  570. foreach ($languages as $oldlang) {
  571. $metatags[$oldlang] = array();
  572. }
  573. }
  574. // Update each of the per-language metatag configurations in turn.
  575. foreach ($metatags as $langcode => $new_metatags) {
  576. // Allow other modules to alter the meta tags prior to saving using
  577. // hook_metatag_presave().
  578. foreach (module_implements('metatag_presave') as $module) {
  579. $function = "{$module}_metatag_presave";
  580. $function($new_metatags, $entity_type, $entity_id, $revision_id, $langcode);
  581. }
  582. // If the data array is empty, there is no data to actually save, so just
  583. // delete the record from the database.
  584. if (empty($new_metatags)) {
  585. db_delete('metatag')
  586. ->condition('entity_type', $entity_type)
  587. ->condition('entity_id', $entity_id)
  588. ->condition('revision_id', $revision_id)
  589. ->condition('language', $langcode)
  590. ->execute();
  591. }
  592. // Otherwise save the data for this entity.
  593. else {
  594. db_merge('metatag')
  595. ->key(array(
  596. 'entity_type' => $entity_type,
  597. 'entity_id' => $entity_id,
  598. 'language' => $langcode,
  599. 'revision_id' => $revision_id,
  600. ))
  601. ->fields(array(
  602. 'data' => serialize($new_metatags),
  603. ))
  604. ->execute();
  605. }
  606. }
  607. // Clear cached data.
  608. metatag_metatags_cache_clear($entity_type, $entity_id);
  609. // Clear the entity cache.
  610. entity_get_controller($entity_type)->resetCache(array($entity_id));
  611. }
  612. /**
  613. * Delete an entity's tags.
  614. *
  615. * @param string $entity_type
  616. * The entity type.
  617. * @param int $entity_id
  618. * The entity's ID.
  619. * @param int $revision_id
  620. * The entity's VID.
  621. * @param string $langcode
  622. * The language ID of the entry to delete. If left blank, all language
  623. * entries for this entity will be deleted.
  624. */
  625. function metatag_metatags_delete($entity_type, $entity_id, $revision_id = NULL, $langcode = NULL) {
  626. $revision_ids = array();
  627. if (!empty($revision_id)) {
  628. $revision_ids[] = $revision_id;
  629. }
  630. return metatag_metatags_delete_multiple($entity_type, array($entity_id), $revision_ids, $langcode);
  631. }
  632. /**
  633. * Delete multiple entities' tags.
  634. *
  635. * @param string $entity_type
  636. * The entity type.
  637. * @param array $entity_ids
  638. * The list of IDs.
  639. * @param array $revision_ids
  640. * An optional list of revision IDs; if omitted all revisions will be deleted.
  641. * @param string $langcode
  642. * The language ID of the entities to delete. If left blank, all language
  643. * entries for the enities will be deleted.
  644. *
  645. * @return bool
  646. * If any problems were encountered will return FALSE, otherwise TRUE.
  647. */
  648. function metatag_metatags_delete_multiple($entity_type, array $entity_ids, array $revision_ids = array(), $langcode = NULL) {
  649. // Double check entity IDs and revision IDs are numeric.
  650. $entity_ids = array_filter($entity_ids, 'is_numeric');
  651. $revision_ids = array_filter($revision_ids, 'is_numeric');
  652. if (!empty($entity_ids) || !empty($revision_ids)) {
  653. $transaction = db_transaction();
  654. try {
  655. // Let other modules know about the records being deleted using
  656. // hook_metatag_metatags_delete().
  657. module_invoke_all('metatag_metatags_delete', $entity_type, $entity_ids, $revision_ids, $langcode);
  658. // Set the entity to delete.
  659. $query = db_delete('metatag')
  660. ->condition('entity_type', $entity_type);
  661. // If revision IDs were specified then just use those in the query.
  662. if (!empty($revision_ids)) {
  663. $query->condition('revision_id', $revision_ids, 'IN');
  664. }
  665. // No revision IDs were specified, so work from the entity IDs.
  666. else {
  667. $query->condition('entity_id', $entity_ids, 'IN');
  668. }
  669. // Limit to a language if one was specified.
  670. if (!empty($langcode)) {
  671. $query->condition('language', $langcode);
  672. }
  673. // Perform the deletion(s).
  674. $query->execute();
  675. // Clear cached data.
  676. metatag_metatags_cache_clear($entity_type, $entity_ids);
  677. // Clear the caches for these entities.
  678. entity_get_controller($entity_type)->resetCache($entity_ids);
  679. return TRUE;
  680. }
  681. catch (Exception $e) {
  682. $transaction->rollback();
  683. watchdog_exception('metatag', $e);
  684. throw $e;
  685. }
  686. }
  687. else {
  688. watchdog('metatag', 'No entity IDs or revision IDs were submitted to metatag_metatags_delete_multiple().');
  689. }
  690. return FALSE;
  691. }
  692. /**
  693. * Clear the cached records for a given entity type or entity ID.
  694. *
  695. * @param string $entity_type
  696. * The entity type to clear.
  697. */
  698. function metatag_metatags_cache_clear($entity_type, $entity_ids = NULL) {
  699. if (empty($entity_ids)) {
  700. cache_clear_all("output:$entity_type", 'cache_metatag', TRUE);
  701. }
  702. else {
  703. if (!is_array($entity_ids)) {
  704. $entity_ids = array($entity_ids);
  705. }
  706. foreach ($entity_ids as $entity_id) {
  707. cache_clear_all("output:$entity_type:$entity_id", 'cache_metatag');
  708. }
  709. }
  710. }
  711. /**
  712. * Implements hook_entity_load().
  713. */
  714. function metatag_entity_load($entities, $entity_type) {
  715. // Wrap this in a try-catch block to work around occasions when the schema
  716. // hasn't been updated yet.
  717. try {
  718. if (metatag_entity_supports_metatags($entity_type)) {
  719. // Get the revision_ids.
  720. $revision_ids = array();
  721. // Track the entity IDs for values to load.
  722. $entity_ids = array();
  723. // Some entities don't support revisions.
  724. $supports_revisions = TRUE;
  725. // Extract the revision ID and verify the entity's bundle is supported.
  726. foreach ($entities as $key => $entity) {
  727. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  728. // Verify that each entity bundle supports Metatag.
  729. if (metatag_entity_supports_metatags($entity_type, $bundle)) {
  730. $entity_ids[] = $entity_id;
  731. if (!empty($revision_id)) {
  732. $revision_ids[] = $revision_id;
  733. }
  734. }
  735. }
  736. // Only proceed if either there were revision IDs identified, or the
  737. // entity doesn't support revisions anyway.
  738. if (!empty($entity_ids)) {
  739. // Load all meta tags for these entities.
  740. $metatags = metatag_metatags_load_multiple($entity_type, $entity_ids, $revision_ids);
  741. // Assign the metatag records for the correct revision ID.
  742. if (!empty($metatags)) {
  743. foreach ($entities as $entity_id => $entity) {
  744. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  745. $revision_id = intval($revision_id);
  746. $entities[$entity_id]->metatags = isset($metatags[$entity_id][$revision_id]) ? $metatags[$entity_id][$revision_id] : array();
  747. }
  748. }
  749. }
  750. }
  751. }
  752. catch (Exception $e) {
  753. 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',
  754. array(
  755. '@update' => base_path() . 'update.php',
  756. '%ids' => implode(', ', array_keys($entities)),
  757. '%type' => $entity_type,
  758. '%error' => $e->getMessage(),
  759. ),
  760. WATCHDOG_WARNING);
  761. // Don't display the same message twice for Drush.
  762. if (drupal_is_cli()) {
  763. drupal_set_message(t('Run your updates, like drush updb.'));
  764. }
  765. // Only message people who can see it in watchdog and can likely fix it.
  766. elseif (user_access('access site reports')) {
  767. 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');
  768. }
  769. }
  770. }
  771. /**
  772. * Implements hook_entity_insert().
  773. */
  774. function metatag_entity_insert($entity, $entity_type) {
  775. if (isset($entity->metatags)) {
  776. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  777. // Verify that this entity type / bundle is supported.
  778. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  779. return;
  780. }
  781. $revision_id = intval($revision_id);
  782. // Determine the entity's language.
  783. $langcode = entity_language($entity_type, $entity);
  784. // Unfortunately due to how core works, the normal entity_language()
  785. // function returns 'und' instead of the node's language during node
  786. // creation.
  787. if ((empty($langcode) || $langcode == LANGUAGE_NONE) && !empty($entity->language)) {
  788. $langcode = $entity->language;
  789. }
  790. // If no language was still found, use the 'no language' value.
  791. if (empty($langcode)) {
  792. $langcode = LANGUAGE_NONE;
  793. }
  794. // Work-around for initial entity creation where a language was selection
  795. // but where it's different to the form's value.
  796. if (!isset($entity->metatags[$langcode]) && isset($entity->metatags[LANGUAGE_NONE])) {
  797. $entity->metatags[$langcode] = $entity->metatags[LANGUAGE_NONE];
  798. unset($entity->metatags[LANGUAGE_NONE]);
  799. }
  800. // Support for Workbench Moderation.
  801. if ($entity_type == 'node' && _metatag_isdefaultrevision($entity)) {
  802. return;
  803. }
  804. metatag_metatags_save($entity_type, $entity_id, $revision_id, $entity->metatags, $bundle);
  805. }
  806. }
  807. /**
  808. * Implements hook_entity_update().
  809. */
  810. function metatag_entity_update($entity, $entity_type) {
  811. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  812. // If this entity object isn't allowed meta tags, don't continue.
  813. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  814. return;
  815. }
  816. $revision_id = intval($revision_id);
  817. if (isset($entity->metatags)) {
  818. // Determine the entity's new language. This will always be accurate as the
  819. // language value will already have been updated by the time this function
  820. // executes, and it will be loaded for the correct edit process.
  821. $new_language = metatag_entity_get_language($entity_type, $entity);
  822. // If applicable, determine the entity's original language. This cannot be
  823. // obtained via the normal API as that data will already have been updated,
  824. // instead check to see if the entity has an old-fasioned 'language' value.
  825. if (isset($entity->original) && isset($entity->language) && isset($entity->original->language)) {
  826. $old_language = $entity->original->language;
  827. // If the language has changed then additional checking needs to be done.
  828. // Need to compare against the entity's raw language value as they will
  829. // be different when updating a translated entity, versus an untranslated
  830. // entity or a source entity for translation, and give a false positive.
  831. if ($new_language == $entity->language && $new_language != $old_language) {
  832. // If this entity is not translated, or if it is translated but the
  833. // translation was previously created, then some language cleanup needs
  834. // to be done.
  835. if (!isset($entity->translation) || (isset($entity->translation) && !empty($entity->translation['created']))) {
  836. // Delete the old language record. This will not affect old revisions.
  837. db_delete('metatag')
  838. ->condition('entity_type', $entity_type)
  839. ->condition('entity_id', $entity_id)
  840. ->condition('revision_id', $revision_id)
  841. ->condition('language', $old_language)
  842. ->execute();
  843. // Swap out the metatag values for the two languages.
  844. if (isset($entity->metatags[$old_language])) {
  845. $entity->metatags[$new_language] = $entity->metatags[$old_language];
  846. unset($entity->metatags[$old_language]);
  847. }
  848. }
  849. }
  850. }
  851. // Support for Workbench Moderation.
  852. if ($entity_type == 'node' && _metatag_isdefaultrevision($entity)) {
  853. return;
  854. }
  855. // Save the record.
  856. metatag_metatags_save($entity_type, $entity_id, $revision_id, $entity->metatags, $bundle);
  857. }
  858. else {
  859. // Still ensure the meta tag output is cached.
  860. metatag_metatags_cache_clear($entity_type, $entity_id);
  861. }
  862. }
  863. /**
  864. * Implements hook_entity_delete().
  865. */
  866. function metatag_entity_delete($entity, $entity_type) {
  867. list($entity_id) = entity_extract_ids($entity_type, $entity);
  868. metatag_metatags_delete($entity_type, $entity_id);
  869. }
  870. /**
  871. * Implements hook_field_attach_delete_revision().
  872. */
  873. function metatag_field_attach_delete_revision($entity_type, $entity) {
  874. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  875. $revision_id = intval($revision_id);
  876. metatag_metatags_delete($entity_type, $entity_id, $revision_id);
  877. }
  878. /**
  879. * Build and alter metatag instance name.
  880. *
  881. * @param object $entity
  882. * The entity object to generate the metatags instance name for.
  883. * @param string $entity_type
  884. * The entity type of the entity.
  885. * @param string $bundle
  886. * The bundle of the entity.
  887. *
  888. * @return string
  889. * The resulting name of the config instance.
  890. */
  891. function metatag_get_entity_metatags_instance($entity, $entity_type, $bundle) {
  892. $instance = "{$entity_type}:{$bundle}";
  893. drupal_alter('metatag_get_entity_metatags_instance', $instance, $entity, $entity_type, $bundle);
  894. return $instance;
  895. }
  896. /**
  897. * Implements hook_entity_view().
  898. *
  899. * Provides additional argument to allow the display to be forced, to work
  900. * around problems elsewhere in the APIs.
  901. */
  902. function metatag_entity_view($entity, $entity_type, $view_mode, $langcode, $force = FALSE) {
  903. // Only run this function once per page load, for an entity which is allowed
  904. // metatags.
  905. static $i_will_say_this_only_once = FALSE;
  906. // Only proceed if this entity object is the page being viewed.
  907. if (_metatag_entity_is_page($entity_type, $entity)) {
  908. // Get the entity's extra information.
  909. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  910. // If this entity object isn't allowed meta tags, don't continue.
  911. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  912. return;
  913. }
  914. // Some API calls need to force the data loading.
  915. if (!$force) {
  916. // Only run this function once per page load.
  917. if ($i_will_say_this_only_once) {
  918. return;
  919. }
  920. $i_will_say_this_only_once = TRUE;
  921. }
  922. // CTools uses 'page_manager' view mode to indicate the full entity display
  923. // page rather than 'full', so streamline the internal processes.
  924. if ($view_mode == 'page_manager') {
  925. $view_mode = 'full';
  926. }
  927. // Generate metatags output.
  928. if ($output = metatag_generate_entity_metatags($entity, $entity_type, $langcode, $view_mode)) {
  929. $instance = metatag_get_entity_metatags_instance($entity, $entity_type, $bundle);
  930. // We need to register the term's metatags, so we can later fetch them.
  931. // @see metatag_page_build().
  932. metatag_page_set_metatags($instance, $output);
  933. }
  934. }
  935. }
  936. /**
  937. * Generate the metatags for a given entity.
  938. *
  939. * @param object $entity
  940. * The entity object to generate the metatags for.
  941. * @param string $entity_type
  942. * The entity type of the entity.
  943. * @param string $langcode
  944. * The language code used for rendering the entity.
  945. * @param string $view_mode
  946. * The view mode the entity is rendered in.
  947. * @param bool $cached
  948. * TRUE if metatags can be loaded from and saved to the cache. FALSE if the
  949. * cache should be bypassed.
  950. *
  951. * @return mixed
  952. * A renderable array of metatags for the given entity.
  953. * If this entity object isn't allowed meta tags, return FALSE (empty).
  954. */
  955. function metatag_generate_entity_metatags($entity, $entity_type, $langcode = NULL, $view_mode = 'full', $cached = TRUE) {
  956. // Obtain some details of the entity that are needed elsewhere.
  957. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  958. // If this entity object isn't allowed meta tags, don't continue.
  959. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  960. return;
  961. }
  962. $revision_id = intval($revision_id);
  963. // Check if a specific metatag config exists, otherwise just use the global
  964. // one, stripping out the bundle.
  965. $instance = metatag_get_entity_metatags_instance($entity, $entity_type, $bundle);
  966. if (!metatag_config_load_with_defaults($instance, FALSE)) {
  967. $instance = "{$entity_type}";
  968. }
  969. // Determine the language this entity actually uses.
  970. $entity_language = metatag_entity_get_language($entity_type, $entity);
  971. // If no language was requested, try the language defined for this page
  972. // request.
  973. if (empty($langcode)) {
  974. $langcode = $GLOBALS['language_content']->language;
  975. }
  976. // This entity doesn't have any languages defined, i.e. it uses 'und'. This
  977. // can't conflict with loading the wrong language as entities either have no
  978. // language or they have specific one(s), they can't have both.
  979. if ($entity_language == LANGUAGE_NONE) {
  980. $langcode = LANGUAGE_NONE;
  981. }
  982. // If there are no meta tags for the currently identified language, and there
  983. // *are* meta tags defined for the entity's default language, use the entity's
  984. // default language's values, unless the "Don't load entity's default
  985. // language values if no languages match" option is enabled on the advanced
  986. // settings page.
  987. elseif (empty($entity->metatags[$langcode]) && !empty($entity->metatags[$entity_language]) && !variable_get('metatag_entity_no_lang_default', FALSE)) {
  988. $langcode = $entity_language;
  989. }
  990. // Other scenarios.
  991. else {
  992. // There's no need to do anything else - either there are meta tag values
  993. // created for the requested language or there aren't.
  994. }
  995. $cid = FALSE;
  996. $key = FALSE;
  997. $metatag_variants = array();
  998. // Caching is enabled.
  999. if ($cached && variable_get('metatag_cache_output', FALSE)) {
  1000. // All possible variants of the metatags for this entity are stored in a
  1001. // single cache entry.
  1002. $cid = "output:$entity_type:$entity_id";
  1003. // All applicable pieces for this current page.
  1004. $key_parts = array(
  1005. 'entity_type' => $entity_type,
  1006. 'bundle' => $bundle,
  1007. 'entity_id' => $entity_id,
  1008. 'revision_id' => $revision_id,
  1009. // Cache separately based on the language of the passed-in entity and the
  1010. // overall active language of the page.
  1011. 'langcode' => $langcode,
  1012. 'language_content' => $GLOBALS['language_content']->language,
  1013. 'view_mode' => $view_mode,
  1014. );
  1015. $key = metatag_cache_default_cid_parts($key_parts);
  1016. if ($cache = metatag_cache_get($cid)) {
  1017. $metatag_variants = $cache->data;
  1018. }
  1019. }
  1020. // If a cached object exists for this key, return it.
  1021. if (!empty($key) && isset($metatag_variants[$key])) {
  1022. $output = $metatag_variants[$key];
  1023. }
  1024. // Otherwise, generate the output tags.
  1025. else {
  1026. // Separate the meta tags.
  1027. $metatags = isset($entity->metatags) ? $entity->metatags : array();
  1028. // Build options for meta tag rendering.
  1029. $options = array(
  1030. 'entity' => $entity,
  1031. 'entity_type' => $entity_type,
  1032. 'view_mode' => $view_mode,
  1033. );
  1034. // Ensure we actually pass a language object rather than language code.
  1035. $languages = language_list();
  1036. if (isset($languages[$langcode])) {
  1037. $options['language'] = $languages[$langcode];
  1038. }
  1039. // Include token replacement data. Don't reload the entity object as doing
  1040. // so would conflict with content editorial workflows.
  1041. if (!empty($entity_id)) {
  1042. $token_type = token_get_entity_mapping('entity', $entity_type);
  1043. $options['token data'][$token_type] = $entity;
  1044. }
  1045. // Render the metatags and save to the cache.
  1046. $output = metatag_metatags_view($instance, $metatags, $options);
  1047. // If output caching is enabled, store the data for later.
  1048. if (!empty($key) && !empty($cid)) {
  1049. $metatag_variants[$key] = $output;
  1050. metatag_cache_set($cid, $metatag_variants);
  1051. }
  1052. }
  1053. return $output;
  1054. }
  1055. /**
  1056. * Generate the metatags for a given entity.
  1057. *
  1058. * @param object $entity_id
  1059. * The entity id of the entity to generate the metatags for.
  1060. * @param string $entity_type
  1061. * The entity type of the entity to generate the metatags for.
  1062. * @param string $langcode
  1063. * The language code used for rendering the entity.
  1064. *
  1065. * @return array
  1066. * A renderable array of metatags for the given entity.
  1067. */
  1068. function metatags_get_entity_metatags($entity_id, $entity_type, $langcode = NULL) {
  1069. $entities = entity_load($entity_type, array($entity_id));
  1070. $entity = reset($entities);
  1071. return !empty($entity) ? metatag_generate_entity_metatags($entity, $entity_type, $langcode) : array();
  1072. }
  1073. /**
  1074. * Build a renderable array of meta tag output.
  1075. *
  1076. * @param string $instance
  1077. * The configuration instance key of the meta tags to use, e.g.
  1078. * "node:article".
  1079. * @param array $metatags
  1080. * An array of meta tag data.
  1081. * @param array $options
  1082. * (optional) An array of options including the following keys and values:
  1083. * - language: A language object.
  1084. * - token data: An array of data to pass into token_replace() during
  1085. * meta tag value generation.
  1086. */
  1087. function metatag_metatags_view($instance, array $metatags = array(), array $options = array()) {
  1088. $output = array();
  1089. // Convert language codes to a language object.
  1090. if (isset($options['language']) && is_string($options['language'])) {
  1091. $languages = language_list();
  1092. $options['language'] = isset($languages[$options['language']]) ? $languages[$options['language']] : NULL;
  1093. }
  1094. if (empty($options['language'])) {
  1095. $options['language'] = $GLOBALS['language_content'];
  1096. }
  1097. // If there are any tags, determine the translation to display.
  1098. if (!empty($metatags)) {
  1099. // Get the display language; default to the entity's language.
  1100. if (isset($options['language']) && isset($options['language']->language) && isset($metatags[$options['language']->language])) {
  1101. $metatags = $metatags[$options['language']->language];
  1102. }
  1103. // If no language requested, use the no-language value.
  1104. elseif (!empty($metatags[LANGUAGE_NONE])) {
  1105. $metatags = $metatags[LANGUAGE_NONE];
  1106. }
  1107. else {
  1108. $metatags = array();
  1109. }
  1110. }
  1111. // Add any default tags to the mix.
  1112. $metatags += metatag_config_load_with_defaults($instance);
  1113. $options['instance'] = $instance;
  1114. // Don't output meta tags that only contain the pager.
  1115. $current_pager = metatag_get_current_pager();
  1116. foreach ($metatags as $metatag => $data) {
  1117. if ((!empty($data['value']) || (isset($data['value']) && is_numeric($data['value'])))
  1118. && $metatag_instance = metatag_get_instance($metatag, $data)) {
  1119. $tag_output = $metatag_instance->getElement($options);
  1120. // Don't output the pager if that's all there is.
  1121. if ($tag_output != $current_pager) {
  1122. $output[$metatag] = $tag_output;
  1123. }
  1124. }
  1125. }
  1126. // Allow the output meta tags to be modified using
  1127. // hook_metatag_metatags_view_alter().
  1128. drupal_alter('metatag_metatags_view', $output, $instance, $options);
  1129. return $output;
  1130. }
  1131. /**
  1132. * Get the pager string for the current page.
  1133. *
  1134. * @return string
  1135. * Returns a string based upon the 'metatag_pager_string' variable and the
  1136. * current page number.
  1137. */
  1138. function metatag_get_current_pager() {
  1139. if (isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page'])) {
  1140. $page = intval($_GET['page']) + 1;
  1141. if ($page > 1) {
  1142. $pager = variable_get('metatag_pager_string', 'Page PAGER | ');
  1143. return str_replace('PAGER', $page, $pager);
  1144. }
  1145. }
  1146. }
  1147. /**
  1148. * Returns metatags values.
  1149. */
  1150. function metatag_metatags_values($instance, array $metatags = array(), array $options = array()) {
  1151. $values = array();
  1152. // Apply defaults to the data for each language.
  1153. foreach ($metatags as $language => $metatag) {
  1154. $metatags[$language] += metatag_config_load_with_defaults($instance);
  1155. }
  1156. // Generate output only if we have a valid language.
  1157. if (isset($options['language']) && is_string($options['language']) && isset($metatags[$options['language']])) {
  1158. $language = $options['language'];
  1159. // Convert language codes to a language object.
  1160. $languages = language_list();
  1161. $options['language'] = isset($languages[$language]) ? $languages[$language] : NULL;
  1162. $options['instance'] = $instance;
  1163. // Get output elements.
  1164. foreach ($metatags[$language] as $metatag => $data) {
  1165. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  1166. $values[$metatag] = $metatag_instance->getValue($options);
  1167. }
  1168. }
  1169. }
  1170. return array_filter($values, 'drupal_strlen');
  1171. }
  1172. /**
  1173. * Build a FAPI array for editing meta tags.
  1174. *
  1175. * @param array $form
  1176. * The current FAPI array.
  1177. * @param string $instance
  1178. * The configuration instance key of the metatags to use, e.g. "node:article".
  1179. * @param array $metatags
  1180. * An array of metatag data.
  1181. * @param array $options
  1182. * (optional) An array of options including the following keys and values:
  1183. * - token types: An array of token types to be passed to theme_token_tree().
  1184. */
  1185. function metatag_metatags_form(array &$form, $instance, array $metatags = array(), array $options = array()) {
  1186. $info = metatag_get_info();
  1187. if (empty($info['tags'])) {
  1188. return;
  1189. }
  1190. // Work out the language code to use, default to NONE.
  1191. $langcode = LANGUAGE_NONE;
  1192. if (!empty($form['#entity_type'])) {
  1193. if (!empty($form['#entity'])) {
  1194. $langcode = metatag_entity_get_language($form['#entity_type'], $form['#entity']);
  1195. }
  1196. else {
  1197. $entity_info = entity_get_info($form['#entity_type']);
  1198. if (!empty($entity_info['token type'])) {
  1199. $entity_key = '#' . $entity_info['token type'];
  1200. if (!empty($form[$entity_key])) {
  1201. $langcode = metatag_entity_get_language($form['#entity_type'], $form[$entity_key]);
  1202. }
  1203. }
  1204. }
  1205. }
  1206. // Merge in the default options.
  1207. $options += array(
  1208. 'token types' => array(),
  1209. 'defaults' => metatag_config_load_with_defaults($instance),
  1210. 'instance' => $instance,
  1211. );
  1212. // Trigger hook_metatag_token_types_alter().
  1213. // Allow the defined tokens to be modified.
  1214. drupal_alter('metatag_token_types', $options);
  1215. $form['metatags'] = array(
  1216. '#type' => 'fieldset',
  1217. '#title' => t('Meta tags'),
  1218. '#collapsible' => TRUE,
  1219. '#collapsed' => TRUE,
  1220. '#multilingual' => TRUE,
  1221. '#tree' => TRUE,
  1222. '#access' => user_access('edit meta tags') || user_access('administer meta tags'),
  1223. '#weight' => 40,
  1224. '#language' => $langcode,
  1225. '#attributes' => array(
  1226. 'class' => array('metatags-form'),
  1227. ),
  1228. );
  1229. $form['metatags'][$langcode] = array(
  1230. '#metatag_defaults' => $options['defaults'],
  1231. '#type' => 'container',
  1232. '#multilingual' => TRUE,
  1233. '#tree' => TRUE,
  1234. );
  1235. // Show a different intro message for entity pages vs config pages.
  1236. if (isset($form['#entity'])) {
  1237. $form['metatags']['intro_text'] = array(
  1238. '#markup' => '<p>' . t('Configure the meta tags below. Tokens, e.g. "[node:summary]", automatically insert the corresponding information from that field or value, which helps to avoid redundant meta data and possible search engine penalization; see the "Browse available tokens" popup for more details.') . '</p>',
  1239. '#weight' => -10,
  1240. );
  1241. }
  1242. else {
  1243. $form['metatags']['intro_text'] = array(
  1244. '#markup' => '<p>' . t('Configure the meta tags below. Use tokens (see the "Browse available tokens" popup) to avoid redundant meta data and search engine penalization. For example, a \'keyword\' value of "example" will be shown on all content using this configuration, whereas using the [node:field_keywords] automatically inserts the "keywords" values from the current entity (node, term, etc).') . '</p>',
  1245. '#weight' => -10,
  1246. );
  1247. }
  1248. // Only support vertical tabs if there is a vertical tab element.
  1249. foreach (element_children($form) as $key) {
  1250. if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
  1251. $form['metatags']['#group'] = $key;
  1252. $form['metatags']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'metatag') . '/metatag.vertical-tabs.js';
  1253. break;
  1254. }
  1255. }
  1256. // Merge in the default meta tag configurations.
  1257. $metatags += $options['defaults'];
  1258. // This will be used later.
  1259. $group_metatag_access = array();
  1260. // Build the form for each metatag.
  1261. foreach ($info['tags'] as $metatag => $metatag_info) {
  1262. // @todo Replace context matching with hook_metatag_access().
  1263. if (isset($options['context']) && isset($metatag_info['context'])) {
  1264. if (!in_array($options['context'], $metatag_info['context'])) {
  1265. continue;
  1266. }
  1267. }
  1268. $metatag_instance = metatag_get_instance($metatag, isset($metatags[$metatag]) ? $metatags[$metatag] : array());
  1269. if (empty($metatag_instance)) {
  1270. continue;
  1271. }
  1272. // Get the form element from the meta tag class.
  1273. $metatag_form = $metatag_instance->getForm($options);
  1274. // Add a default value form element.
  1275. if (isset($options['defaults'][$metatag]['value'])) {
  1276. $metatag_form['default'] = array(
  1277. '#type' => 'hidden',
  1278. '#value' => $options['defaults'][$metatag]['value'],
  1279. );
  1280. }
  1281. // Optional extended edit permissions.
  1282. if (variable_get('metatag_extended_permissions', FALSE)) {
  1283. $metatag_form['#access'] = user_access('edit meta tag: ' . $metatag) || user_access('administer meta tags');
  1284. }
  1285. else {
  1286. $metatag_form['#access'] = $form['metatags']['#access'];
  1287. }
  1288. if (!empty($metatag_info['group'])) {
  1289. $group_key = $metatag_info['group'];
  1290. if (isset($info['groups'][$group_key]['label']) && !isset($form['metatags'][$langcode][$group_key])) {
  1291. $group = $info['groups'][$group_key] + array('form' => array(), 'description' => NULL);
  1292. $form['metatags'][$langcode][$group_key] = $group['form'] + array(
  1293. '#type' => 'fieldset',
  1294. '#title' => $group['label'],
  1295. '#description' => !empty($group['description']) ? $group['description'] : '',
  1296. '#collapsible' => TRUE,
  1297. '#collapsed' => TRUE,
  1298. );
  1299. }
  1300. $form['metatags'][$langcode][$group_key][$metatag] = $metatag_form + array(
  1301. '#parents' => array('metatags', $langcode, $metatag),
  1302. );
  1303. // Hide the fieldset itself if there is not at least one of the meta tag
  1304. // fields visible.
  1305. if (variable_get('metatag_extended_permissions', FALSE)) {
  1306. $form['metatags'][$langcode][$group_key]['#access'] = count(element_get_visible_children($form['metatags'][$langcode][$group_key])) > 0;
  1307. }
  1308. else {
  1309. $form['metatags'][$langcode][$group_key]['#access'] = $form['metatags']['#access'];
  1310. }
  1311. // Structure the access parameter into this array, and make use of it
  1312. // later when we move on. Besides, this foreach is getting heavy.
  1313. $group_metatag_access[$group_key] = $form['metatags'][$langcode][$group_key]['#access'];
  1314. }
  1315. else {
  1316. $form['metatags'][$langcode][$metatag] = $metatag_form;
  1317. }
  1318. }
  1319. // Hide the fieldset itself if there is not at least one of the meta tag
  1320. // fields visible; only bother checking this if the user had edit access in
  1321. // the first place.
  1322. if ($form['metatags']['#access'] && variable_get('metatag_extended_permissions', FALSE)) {
  1323. $form['metatags']['#access'] = count(element_get_visible_children($form['metatags'][$langcode])) > 0;
  1324. }
  1325. // Check the #access of each group. If it passed, we display options for
  1326. // tokens. By this we update the #description of each group.
  1327. if ($form['metatags']['#access']) {
  1328. // Check if each meta tag group is being displayed.
  1329. if (!empty($group_metatag_access)) {
  1330. // Built the token browser link. For value "all" theme_token_tree()
  1331. // compares with string, not array.
  1332. if (in_array('all', $options['token types'])) {
  1333. $options['token types'] = 'all';
  1334. }
  1335. $token_listing_link = theme('token_tree',
  1336. array(
  1337. 'token_types' => $options['token types'],
  1338. 'dialog' => TRUE,
  1339. )
  1340. );
  1341. foreach ($group_metatag_access as $group_key => $token_access) {
  1342. if ($token_access) {
  1343. // Update the description.
  1344. if (isset($form['metatags'][$langcode][$group_key]['#description'])) {
  1345. $form['metatags'][$langcode][$group_key]['#description'] .= '<br />';
  1346. }
  1347. else {
  1348. $form['metatags'][$langcode][$group_key]['#description'] = '';
  1349. }
  1350. // Add the token browser popup link.
  1351. $form['metatags'][$langcode][$group_key]['#description'] .= $token_listing_link;
  1352. }
  1353. }
  1354. }
  1355. }
  1356. // Add a submit handler to compare the submitted values against the default
  1357. // values.
  1358. $form += array('#submit' => array());
  1359. if (module_exists('commerce') && isset($form['#entity_type']) && $form['#entity_type'] == 'commerce_product') {
  1360. $form['actions']['submit']['#submit'][] = 'metatag_commerce_product_form_submit';
  1361. }
  1362. else {
  1363. array_unshift($form['#submit'], 'metatag_metatags_form_submit');
  1364. }
  1365. }
  1366. /**
  1367. * Form API submission callback.
  1368. *
  1369. * Unset meta tag values that equal their default values, and load any
  1370. * additional meta tag values for other languages so that they can be properly
  1371. * saved later on.
  1372. *
  1373. * @see metatag_metatags_save()
  1374. */
  1375. function metatag_metatags_form_submit($form, &$form_state) {
  1376. if (!empty($form_state['values']['metatags'])) {
  1377. // Unset meta tag values that equal their default values.
  1378. foreach ($form_state['values']['metatags'] as $langcode => $values) {
  1379. if (!empty($form['metatags'][$langcode]['#metatag_defaults'])) {
  1380. metatag_filter_values_from_defaults($form_state['values']['metatags'][$langcode], $form['metatags'][$langcode]['#metatag_defaults']);
  1381. }
  1382. }
  1383. // Need to load the entity's values for other languages, otherwise they will
  1384. // be incorrectly deleted later on.
  1385. if (isset($form['#entity']) && !empty($form['#entity']->metatags)) {
  1386. foreach ($form['#entity']->metatags as $langcode => $values) {
  1387. if (!isset($form_state['values']['metatags'][$langcode])) {
  1388. $form_state['values']['metatags'][$langcode] = $values;
  1389. }
  1390. }
  1391. }
  1392. }
  1393. }
  1394. /**
  1395. * Form API submission callback for Commerce product.
  1396. *
  1397. * Unlike metatag_metatags_form_submit.
  1398. *
  1399. * @see metatag_metatags_save()
  1400. */
  1401. function metatag_commerce_product_form_submit($form, &$form_state) {
  1402. // Trigger the normal meta tag form submission.
  1403. metatag_metatags_form_submit($form, $form_state);
  1404. // The entity being saved.
  1405. $entity_type = 'commerce_product';
  1406. $product = $form_state[$entity_type];
  1407. $entity_id = $product->product_id;
  1408. $revision_id = $product->revision_id;
  1409. // Get the full entity details.
  1410. list(, , $bundle) = entity_extract_ids($entity_type, $product);
  1411. // Update the meta tags for this entity type.
  1412. metatag_metatags_save($entity_type, $entity_id, $revision_id, $form_state['values']['metatags'], $bundle);
  1413. }
  1414. /**
  1415. * Implements hook_field_extra_fields().
  1416. */
  1417. function metatag_field_extra_fields() {
  1418. $extra = array();
  1419. foreach (entity_get_info() as $entity_type => $entity_info) {
  1420. if (!empty($entity_info['bundles'])) {
  1421. foreach (array_keys($entity_info['bundles']) as $bundle) {
  1422. if (metatag_entity_supports_metatags($entity_type, $bundle)) {
  1423. $extra[$entity_type][$bundle]['form']['metatags'] = array(
  1424. 'label' => t('Meta tags'),
  1425. 'description' => t('Meta tag module form elements.'),
  1426. 'weight' => 40,
  1427. );
  1428. }
  1429. }
  1430. }
  1431. }
  1432. return $extra;
  1433. }
  1434. /**
  1435. * Check whether the requested entity type (and bundle) support metatag.
  1436. *
  1437. * By default the entities are disabled, only certain entities will have been
  1438. * enabled during installation. If an entity type is enabled it is assumed that
  1439. * the entity bundles will also be enabled by default.
  1440. *
  1441. * @see metatag_entity_type_is_suitable()
  1442. */
  1443. function metatag_entity_supports_metatags($entity_type = NULL, $bundle = NULL) {
  1444. $entity_types = &drupal_static(__FUNCTION__);
  1445. // Identify which entities & bundles are supported the first time the
  1446. // function is called.
  1447. if (!isset($entity_types)) {
  1448. foreach (entity_get_info() as $entity_name => $entity_info) {
  1449. // Verify that this entity type is suitable.
  1450. $entity_types[$entity_name] = metatag_entity_type_is_suitable($entity_name, $entity_info);
  1451. // The entity type technically supports entities.
  1452. if (!empty($entity_types[$entity_name])) {
  1453. // Entiy types are enabled by default.
  1454. // Allow entities to be disabled by assigning a variable
  1455. // 'metatag_enable_{$entity_type}' the value FALSE, e.g.:
  1456. //
  1457. // // Disable metatags for file_entity.
  1458. // $conf['metatag_enable_file'] = FALSE;.
  1459. //
  1460. // @see Settings page.
  1461. if (variable_get('metatag_enable_' . $entity_name, FALSE) == FALSE) {
  1462. $entity_types[$entity_name] = FALSE;
  1463. }
  1464. // Check each bundle.
  1465. else {
  1466. $entity_types[$entity_name] = array();
  1467. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  1468. // If only one bundle exists, take configuration for entity, to
  1469. // reflect options as they are available in the UI.
  1470. if (count($entity_info['bundles']) === 1) {
  1471. $entity_types[$entity_name][$bundle_name] = variable_get('metatag_enable_' . $entity_name, FALSE) == TRUE;
  1472. continue;
  1473. }
  1474. // Allow bundles to be disabled by assigning a variable
  1475. // 'metatag_enable_{$entity_type}__{$bundle}' the value FALSE, e.g.:
  1476. //
  1477. // // Disable metatags for carousel nodes.
  1478. // $conf['metatag_enable_node__carousel'] = FALSE;.
  1479. //
  1480. // @see Settings page.
  1481. if (variable_get('metatag_enable_' . $entity_name . '__' . $bundle_name, TRUE) == FALSE) {
  1482. $entity_types[$entity_name][$bundle_name] = FALSE;
  1483. }
  1484. else {
  1485. $entity_types[$entity_name][$bundle_name] = TRUE;
  1486. }
  1487. }
  1488. }
  1489. }
  1490. }
  1491. }
  1492. // It was requested to check a specific entity.
  1493. if (isset($entity_type)) {
  1494. // It was also requested to check a specific bundle for this entity.
  1495. if (isset($bundle)) {
  1496. $supported = !empty($entity_types[$entity_type][$bundle]);
  1497. }
  1498. // Check the entity.
  1499. else {
  1500. $supported = !empty($entity_types[$entity_type]);
  1501. }
  1502. return $supported;
  1503. }
  1504. // If nothing specific was requested, return the complete list of supported
  1505. // entities & bundles.
  1506. return $entity_types;
  1507. }
  1508. /**
  1509. * Enable support for a specific entity type if setting does not exist.
  1510. *
  1511. * @param string $entity_type
  1512. * The entity type.
  1513. * @param string $bundle
  1514. * The bundle of the entity.
  1515. * @param bool $force_enable
  1516. * If TRUE, then the type is enabled regardless of any stored variables.
  1517. *
  1518. * @return bool
  1519. * TRUE if either the bundle or entity type was enabled by this function.
  1520. */
  1521. function metatag_entity_type_enable($entity_type, $bundle = NULL, $force_enable = FALSE) {
  1522. // The bundle was defined.
  1523. $bundle_set = FALSE;
  1524. if (isset($bundle)) {
  1525. $stored_bundle = variable_get('metatag_enable_' . $entity_type . '__' . $bundle, NULL);
  1526. if ($force_enable || !isset($stored_bundle)) {
  1527. variable_set('metatag_enable_' . $entity_type . '__' . $bundle, TRUE);
  1528. $bundle_set = TRUE;
  1529. }
  1530. }
  1531. // Always enable the entity type, because otherwise there's no point in
  1532. // enabling the bundle.
  1533. $entity_type_set = FALSE;
  1534. $stored_entity_type = variable_get('metatag_enable_' . $entity_type, NULL);
  1535. if ($force_enable || !isset($stored_entity_type)) {
  1536. variable_set('metatag_enable_' . $entity_type, TRUE);
  1537. $entity_type_set = TRUE;
  1538. }
  1539. // Clear the static cache so that the entity type / bundle will work.
  1540. drupal_static_reset('metatag_entity_supports_metatags');
  1541. return $bundle_set || $entity_type_set;
  1542. }
  1543. /**
  1544. * Disable support for a specific entity type.
  1545. *
  1546. * @param string $entity_type
  1547. * The entity type.
  1548. * @param string $bundle
  1549. * The bundle of the entity.
  1550. */
  1551. function metatag_entity_type_disable($entity_type, $bundle = NULL) {
  1552. // The bundle was defined.
  1553. if (isset($bundle)) {
  1554. variable_set('metatag_enable_' . $entity_type . '__' . $bundle, FALSE);
  1555. }
  1556. // The bundle was not defined.
  1557. else {
  1558. variable_set('metatag_enable_' . $entity_type, FALSE);
  1559. }
  1560. // Clear the static cache so that the entity type / bundle will work.
  1561. drupal_static_reset('metatag_entity_supports_metatags');
  1562. }
  1563. /**
  1564. * Add meta tags to be added later with metatag_page_build().
  1565. *
  1566. * @param string $instance
  1567. * The configuration instance key of the meta tags, e.g. "node:article".
  1568. * @param array $metatags
  1569. * An array of meta tags from metatag_metatags_view().
  1570. */
  1571. function metatag_page_set_metatags($instance, $metatags) {
  1572. $page_metatags = &drupal_static(__FUNCTION__, array());
  1573. $page_metatags[$instance] = $metatags;
  1574. }
  1575. /**
  1576. * Retrieve the array of meta tags to be added with metatag_page_build().
  1577. */
  1578. function metatag_page_get_metatags() {
  1579. // @todo Add alter to this result?
  1580. return drupal_static('metatag_page_set_metatags', array());
  1581. }
  1582. /**
  1583. * Implements hook_page_build().
  1584. */
  1585. function metatag_page_build(&$page) {
  1586. // By default do not add meta tags to admin pages. To enable meta tags on
  1587. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  1588. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  1589. return;
  1590. }
  1591. // Special consideration for the Me module, which uses the "user/me" path and
  1592. // will cause problems.
  1593. if (arg(0) == 'user' && arg(1) == 'me' && function_exists('me_menu_alter')) {
  1594. return;
  1595. }
  1596. // The page region can be changed.
  1597. $region = variable_get('metatag_page_region', 'content');
  1598. // Ensure these arrays exist, otherwise several use cases will fail.
  1599. if (!isset($page[$region]) || !is_array($page[$region])) {
  1600. $page[$region] = array();
  1601. }
  1602. if (!isset($page[$region]['metatags']) || !is_array($page[$region]['metatags'])) {
  1603. $page[$region]['metatags'] = array();
  1604. }
  1605. // The front page has special consideration. Also, check if this is an error
  1606. // (403/404) page, those also require separate handling.
  1607. $instance = 'global:frontpage';
  1608. if ((drupal_is_front_page() && metatag_config_is_enabled($instance))
  1609. || ($instance = metatag_is_error_page())) {
  1610. // Generate the cache ID.
  1611. $cid_parts = array(
  1612. 'instance' => $instance,
  1613. );
  1614. $cid = metatag_cache_default_cid_parts($cid_parts);
  1615. if ($cache = metatag_cache_get($cid)) {
  1616. $metatags = $cache->data;
  1617. }
  1618. else {
  1619. $metatags = metatag_metatags_view($instance, array());
  1620. // If output caching is enabled, save this for later.
  1621. if (variable_get('metatag_cache_output', FALSE)) {
  1622. metatag_cache_set($cid, $metatags);
  1623. }
  1624. }
  1625. $page[$region]['metatags'][$instance] = $metatags;
  1626. }
  1627. // Load any meta tags assigned via metatag_page_set_metatags(). Note: this
  1628. // must include the necessary defaults.
  1629. else {
  1630. $page[$region]['metatags'] += metatag_page_get_metatags();
  1631. }
  1632. // If no meta tags were loaded at least load the global defaults. This may be
  1633. // disabled, see README.txt for details.
  1634. if (empty($page[$region]['metatags']) && variable_get('metatag_load_all_pages', TRUE)) {
  1635. $instance = 'global';
  1636. // Generate the cache ID.
  1637. $cid_parts = array(
  1638. 'instance' => $instance,
  1639. 'path' => request_path(),
  1640. );
  1641. $cid = metatag_cache_default_cid_parts($cid_parts);
  1642. if ($cache = metatag_cache_get($cid)) {
  1643. $metatags = $cache->data;
  1644. }
  1645. else {
  1646. $metatags = metatag_metatags_view($instance, array());
  1647. // If output caching is enabled, save this for later.
  1648. if (variable_get('metatag_cache_output', FALSE)) {
  1649. metatag_cache_set($cid, $metatags);
  1650. }
  1651. }
  1652. $page[$region]['metatags'][$instance] = $metatags;
  1653. }
  1654. }
  1655. /**
  1656. * Returns whether the current page is the page of the passed in entity.
  1657. *
  1658. * @param string $entity_type
  1659. * The entity type; e.g. 'node' or 'user'.
  1660. * @param object $entity
  1661. * The entity object.
  1662. *
  1663. * @return mixed
  1664. * TRUE if the current page is the page of the specified entity, or FALSE
  1665. * otherwise. If $entity_type == 'comment' return empty (FALSE).
  1666. */
  1667. function _metatag_entity_is_page($entity_type, $entity) {
  1668. // Exclude comment entities as this conflicts with comment_fragment.module.
  1669. if ($entity_type == 'comment') {
  1670. return;
  1671. }
  1672. $uri = entity_uri($entity_type, $entity);
  1673. $current_path = current_path();
  1674. // Support for Workbench Moderation - if this is a node, check if the content
  1675. // type supports moderation.
  1676. if ($entity_type == 'node' && function_exists('workbench_moderation_node_type_moderated') && workbench_moderation_node_type_moderated($entity->type) === TRUE) {
  1677. return !empty($uri['path']) && ($current_path == $uri['path'] || $current_path == $uri['path'] . '/draft');
  1678. }
  1679. // Support for core node revisions.
  1680. elseif (!empty($uri['path']) && strpos($current_path, $uri['path']) === 0 && strpos($current_path, '/revisions/') && strpos($current_path, '/view')) {
  1681. return TRUE;
  1682. }
  1683. // Any other page.
  1684. else {
  1685. return !empty($uri['path']) && $current_path == $uri['path'];
  1686. }
  1687. }
  1688. /**
  1689. * Implements hook_field_attach_rename_bundle().
  1690. */
  1691. function metatag_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  1692. $instance_old = $entity_type . ':' . $bundle_old;
  1693. $instance_new = $entity_type . ':' . $bundle_new;
  1694. if ($config = metatag_config_load($instance_old)) {
  1695. $config->instance = $instance_new;
  1696. metatag_config_save($config);
  1697. $config->instance = $instance_old;
  1698. metatag_config_delete($config);
  1699. }
  1700. }
  1701. /**
  1702. * Implements hook_field_attach_delete_bundle().
  1703. */
  1704. function metatag_field_attach_delete_bundle($entity_type, $bundle) {
  1705. $instance = $entity_type . ':' . $bundle;
  1706. if ($config = metatag_config_load($instance)) {
  1707. metatag_config_delete($config);
  1708. }
  1709. }
  1710. /**
  1711. * Implements hook_field_attach_form().
  1712. */
  1713. function metatag_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  1714. // Entity_Translation will trigger this hook again, skip it.
  1715. if (!empty($form_state['entity_translation']['is_translation'])) {
  1716. return;
  1717. }
  1718. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  1719. if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
  1720. return;
  1721. }
  1722. $instance = metatag_get_entity_metatags_instance($entity, $entity_type, $bundle);
  1723. // Grab the meta tags for display in the form if there are any.
  1724. if (!empty($entity->metatags)) {
  1725. // Identify the language to use with this entity.
  1726. $entity_language = metatag_entity_get_language($entity_type, $entity);
  1727. // If this is a new translation using Entity Translation, load the meta
  1728. // tags from the entity's original language.
  1729. 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()])) {
  1730. $metatags = $entity->metatags[$handler->getSourceLanguage()];
  1731. }
  1732. // Determine from where we should get the tags.
  1733. elseif (isset($entity->metatags[$langcode])) {
  1734. // Set the tags to the translation set matching that of the form.
  1735. $metatags = $entity->metatags[$langcode];
  1736. }
  1737. // There is no translation for this entity's tags in the current
  1738. // language. Instead, display tags in the language of the entity, the
  1739. // source language of translations. The will provide translators with the
  1740. // original text to translate.
  1741. elseif (isset($entity->metatags[$entity_language])) {
  1742. $metatags = $entity->metatags[$entity_language];
  1743. }
  1744. // This is a preview so set the tags to the raw submission data. No
  1745. // language has been set.
  1746. else {
  1747. $metatags = $entity->metatags;
  1748. }
  1749. }
  1750. else {
  1751. $metatags = array();
  1752. }
  1753. // Certain circumstances can result in $metatags not being an array.
  1754. if (!is_array($metatags)) {
  1755. $metatags = array();
  1756. }
  1757. $options['token types'] = array(
  1758. token_get_entity_mapping('entity', $entity_type),
  1759. );
  1760. $options['context'] = $entity_type;
  1761. // @todo Remove metatag_form_alter() when https://www.drupal.org/node/1284642 is fixed in core.
  1762. // metatag_metatags_form($form, $instance, $metatags, $options);
  1763. $form['#metatags'] = array(
  1764. 'instance' => $instance,
  1765. 'metatags' => $metatags,
  1766. 'options' => $options,
  1767. );
  1768. }
  1769. /**
  1770. * Implements hook_form_alter().
  1771. *
  1772. * @todo Remove this when https://www.drupal.org/node/1284642 is fixed in core.
  1773. */
  1774. function metatag_form_alter(&$form, $form_state, $form_id) {
  1775. if (!empty($form['#metatags']) && !isset($form['metatags'])) {
  1776. extract($form['#metatags']);
  1777. metatag_metatags_form($form, $instance, $metatags, $options);
  1778. unset($form['#metatags']);
  1779. }
  1780. }
  1781. /**
  1782. * Get the meta tag information array of a meta tag.
  1783. *
  1784. * @param string $name
  1785. * The meta tag name, e.g. description, for which the info shall be returned,
  1786. * or NULL to return an array with info about all meta tags.
  1787. */
  1788. function metatag_get_info($type = NULL, $name = NULL) {
  1789. // Use the advanced drupal_static() pattern, since this is called very often.
  1790. static $drupal_static_fast;
  1791. if (!isset($drupal_static_fast)) {
  1792. $drupal_static_fast['metatag_info'] = &drupal_static(__FUNCTION__);
  1793. }
  1794. $info = &$drupal_static_fast['metatag_info'];
  1795. global $language;
  1796. if (!isset($info)) {
  1797. // hook_metatag_info() includes translated strings, so each language is
  1798. // cached separately.
  1799. $cid = 'info:' . $language->language;
  1800. if ($cache = metatag_cache_get($cid)) {
  1801. $info = $cache->data;
  1802. }
  1803. else {
  1804. // Obtain all metatag specs defined in other modules using
  1805. // hook_metatag_info().
  1806. $info = module_invoke_all('metatag_info');
  1807. $info += array('tags' => array(), 'groups' => array());
  1808. // Merge in default values.
  1809. foreach ($info['tags'] as $key => $data) {
  1810. $info['tags'][$key] += array(
  1811. // Merge in default values.
  1812. 'name' => $key,
  1813. 'class' => 'DrupalTextMetaTag',
  1814. );
  1815. }
  1816. // Let other modules alter the entity info using
  1817. // hook_metatag_info_alter().
  1818. drupal_alter('metatag_info', $info);
  1819. metatag_cache_set($cid, $info);
  1820. }
  1821. }
  1822. if (isset($type) && isset($name)) {
  1823. return isset($info[$type][$name]) ? $info[$type][$name] : FALSE;
  1824. }
  1825. elseif (isset($type)) {
  1826. return isset($info[$type]) ? $info[$type] : array();
  1827. }
  1828. else {
  1829. return $info;
  1830. }
  1831. }
  1832. /**
  1833. * Return instance of metatag.
  1834. */
  1835. function metatag_get_instance($metatag, array $data = array()) {
  1836. $info = metatag_get_info('tags', $metatag);
  1837. if (!empty($info['class']) && class_exists($info['class'])) {
  1838. $class = $info['class'];
  1839. return new $class($info, $data);
  1840. }
  1841. }
  1842. /**
  1843. * Return the string value of a meta tag.
  1844. *
  1845. * @param string $metatag
  1846. * The meta tag string.
  1847. * @param array $data
  1848. * The array of data for the meta tag class instance.
  1849. * @param array $options
  1850. * An optional array of additional options to pass to the getValue() method
  1851. * of the meta tag class instance.
  1852. * - raw: A boolean if TRUE will not perform token replacement.
  1853. *
  1854. * @return string
  1855. * A string value.
  1856. */
  1857. function metatag_get_value($metatag, array $data, array $options = array()) {
  1858. $value = '';
  1859. if ($metatag_instance = metatag_get_instance($metatag, $data)) {
  1860. $options["instance"] = $metatag;
  1861. $value = $metatag_instance->getValue($options);
  1862. if (is_array($value)) {
  1863. $value = implode(', ', $value);
  1864. }
  1865. }
  1866. return $value;
  1867. }
  1868. /**
  1869. * Set a variable to be altered in metatag_preprocess_html().
  1870. *
  1871. * @see metatag_get_preprocess_variables()
  1872. * @see metatag_preprocess_html()
  1873. * @see metatag_preprocess_maintenance_page()
  1874. */
  1875. function metatag_set_preprocess_variable($hook, $variable, $value) {
  1876. $variables = &drupal_static(__FUNCTION__, array());
  1877. $variables[$hook][$variable] = $value;
  1878. }
  1879. /**
  1880. * Return an array of variables to be altered in preprocess functions.
  1881. *
  1882. * @see metatag_set_preprocess_variable()
  1883. * @see metatag_preprocess_html()
  1884. * @see metatag_preprocess_maintenance_page()
  1885. */
  1886. function metatag_get_preprocess_variables($hook) {
  1887. $variables = drupal_static('metatag_set_preprocess_variable', array());
  1888. return isset($variables[$hook]) ? $variables[$hook] : array();
  1889. }
  1890. /**
  1891. * Implements hook_preprocess_html().
  1892. */
  1893. function metatag_preprocess_html(&$variables) {
  1894. foreach (metatag_get_preprocess_variables('html') as $variable => $value) {
  1895. $variables[$variable] = $value;
  1896. }
  1897. }
  1898. /**
  1899. * Implements hook_preprocess_maintenance_page().
  1900. */
  1901. function metatag_preprocess_maintenance_page(&$variables) {
  1902. foreach (metatag_get_preprocess_variables('html') as $variable => $value) {
  1903. $variables[$variable] = $value;
  1904. }
  1905. }
  1906. /**
  1907. * Implements hook_html_head_alter().
  1908. *
  1909. * Hide tags added by core that are now handled by metatag.
  1910. */
  1911. function metatag_html_head_alter(&$elements) {
  1912. $metatags = array();
  1913. $other_tags = array();
  1914. foreach ($elements as $key => $data) {
  1915. // Identify meta tags added by the Metatag module.
  1916. if (strpos($key, 'metatag_') === 0) {
  1917. $metatags[] = $key;
  1918. }
  1919. // Identify meta tags added by other modules.
  1920. else {
  1921. $other_tags[] = $key;
  1922. }
  1923. }
  1924. // The meta tag keys to look for.
  1925. $metatag_keys = array('name', 'property');
  1926. // Attributes to look for.
  1927. $attributes = array('name', 'rel');
  1928. // Look for meta tags that were added by other modules and hide them.
  1929. foreach ($metatags as $metatag_name) {
  1930. $metatag = &$elements[$metatag_name];
  1931. // Setting the #access attribute to these will stop them from being output
  1932. // but still leave the tags present for other modules to interact with.
  1933. foreach ($other_tags as $other_tag) {
  1934. $other = &$elements[$other_tag];
  1935. // Look for other meta tags that have one of the defined attributes which
  1936. // matches one of the values from Metatag's tag.
  1937. foreach ($attributes as $attribute) {
  1938. if (isset($other['#attributes'], $other['#attributes'][$attribute]) && is_string($other['#attributes'][$attribute])) {
  1939. foreach ($metatag_keys as $metatag_key) {
  1940. if (isset($metatag[$metatag_key])) {
  1941. if (strtolower($other['#attributes'][$attribute]) == $metatag[$metatag_key]) {
  1942. $other['#access'] = FALSE;
  1943. }
  1944. }
  1945. }
  1946. }
  1947. }
  1948. }
  1949. }
  1950. // If the 'leave core tags' option is disabled then the following meta tags
  1951. // will be removed if they're provided by core.
  1952. if (!variable_get('metatag_leave_core_tags', FALSE)) {
  1953. $core_tags = array(
  1954. 'generator',
  1955. 'canonical',
  1956. 'shortlink',
  1957. // Leave the shortcut icon, that's more of a theming thing.
  1958. // 'shortcut icon',.
  1959. );
  1960. foreach ($elements as $name => &$element) {
  1961. // Ignore meta tags provided by Metatag.
  1962. if (strpos($name, 'metatag_') === 0) {
  1963. continue;
  1964. }
  1965. // Setting the #access attribute to these will stop them from being output
  1966. // but still leave the tags present for other modules to interact with.
  1967. foreach ($core_tags as $tag) {
  1968. if (!empty($element['#attributes']['rel']) && $element['#attributes']['rel'] == $tag) {
  1969. $element['#access'] = FALSE;
  1970. }
  1971. elseif (!empty($element['#attributes']['name']) && strtolower($element['#attributes']['name']) == $tag) {
  1972. $element['#access'] = FALSE;
  1973. }
  1974. }
  1975. }
  1976. }
  1977. }
  1978. /**
  1979. * Implements hook_get_form().
  1980. */
  1981. function metatag_metatag_get_form($metatag, array $data = array(), array $options = array()) {
  1982. $instance = metatag_get_instance($metatag, $data);
  1983. return $instance->getForm($options);
  1984. }
  1985. /**
  1986. * Returns Instance info if exists otherwise return FALSE.
  1987. */
  1988. function metatag_config_instance_info($instance = NULL) {
  1989. global $language;
  1990. $info = &drupal_static(__FUNCTION__);
  1991. // hook_metatag_info() includes translated strings, so each language is cached
  1992. // separately.
  1993. $cid = 'metatag:config:instance:info:' . $language->language;
  1994. if (!isset($info)) {
  1995. if ($cache = metatag_cache_get($cid)) {
  1996. $info = $cache->data;
  1997. }
  1998. else {
  1999. // Allow modules to act upon the record insertion using
  2000. // hook_metatag_config_instance_info().
  2001. $info = module_invoke_all('metatag_config_instance_info');
  2002. // Allow other modules to customize the data using
  2003. // hook_metatag_config_instance_info_alter().
  2004. drupal_alter('metatag_config_instance_info', $info);
  2005. metatag_cache_set($cid, $info);
  2006. }
  2007. }
  2008. if (isset($instance)) {
  2009. return isset($info[$instance]) ? $info[$instance] : FALSE;
  2010. }
  2011. else {
  2012. return $info;
  2013. }
  2014. }
  2015. /**
  2016. * Filter out meta tag values that equal the default values.
  2017. *
  2018. * @todo Use information in $values[$metatag]['default'] rather than a $defaults parameter.
  2019. */
  2020. function metatag_filter_values_from_defaults(array &$values, array $defaults = array()) {
  2021. foreach ($values as $metatag => $data) {
  2022. $default = isset($data['default']) ? $data['default'] : (isset($defaults[$metatag]['value']) ? $defaults[$metatag]['value'] : NULL);
  2023. if (isset($default) && isset($data['value']) && $default === $data['value']) {
  2024. // Meta tag has a default, and it matches user-submitted value.
  2025. unset($values[$metatag]);
  2026. }
  2027. elseif (!isset($default) && (is_string($data['value']) && !drupal_strlen($data['value']) || (is_array($data['value']) && !array_filter($data['value'])))) {
  2028. // Metatag does not have a default, and user did not submit a value.
  2029. unset($values[$metatag]);
  2030. }
  2031. if (isset($values[$metatag]['default'])) {
  2032. // Unset the default hidden value.
  2033. unset($values[$metatag]['default']);
  2034. }
  2035. }
  2036. }
  2037. /**
  2038. * Return all the parents of a given configuration instance.
  2039. *
  2040. * @param string $instance
  2041. * A meta tag configuration instance.
  2042. *
  2043. * @return array
  2044. * An array of instances starting with the $instance parameter, with the end
  2045. * of the array being the global instance.
  2046. */
  2047. function metatag_config_get_parent_instances($instance, $include_global = TRUE) {
  2048. $parents = array();
  2049. $segments = explode(':', $instance);
  2050. while (count($segments) > 0) {
  2051. $parents[] = implode(':', $segments);
  2052. array_pop($segments);
  2053. }
  2054. if ($include_global && end($parents) !== 'global') {
  2055. $parents[] = 'global';
  2056. }
  2057. reset($parents);
  2058. return $parents;
  2059. }
  2060. /**
  2061. * Get the proper label of a configuration instance.
  2062. *
  2063. * @param string $instance
  2064. * A meta tag configuration instance.
  2065. */
  2066. function metatag_config_instance_label($instance) {
  2067. $labels = &drupal_static(__FUNCTION__, array());
  2068. if (!isset($labels[$instance])) {
  2069. $instance_parts = explode(':', $instance);
  2070. $instance_part = array_pop($instance_parts);
  2071. if ($context = metatag_config_instance_info($instance)) {
  2072. $labels[$instance] = $context['label'];
  2073. }
  2074. else {
  2075. $labels[$instance] = t('Unknown (@instance)', array('@instance' => $instance_part));
  2076. }
  2077. // Normally the following would use metatag_config_get_parent_instances()
  2078. // but since we already sliced the instance by separator and removed the
  2079. // last segment, putting the array back together gives us this instance's
  2080. // parent.
  2081. if (!empty($instance_parts)) {
  2082. $labels[$instance] = metatag_config_instance_label(implode(':', $instance_parts)) . ': ' . $labels[$instance];
  2083. }
  2084. }
  2085. return $labels[$instance];
  2086. }
  2087. /**
  2088. * Title callback for meta tag configuration instances.
  2089. */
  2090. function metatag_config_title($config) {
  2091. return metatag_config_instance_label($config->instance);
  2092. }
  2093. /**
  2094. * Access callback for meta tag configuration instances.
  2095. */
  2096. function metatag_config_access($op, $config = NULL) {
  2097. if (!user_access('administer meta tags')) {
  2098. return FALSE;
  2099. }
  2100. if ($op == 'enable') {
  2101. return !empty($config->disabled);
  2102. }
  2103. elseif ($op == 'disable') {
  2104. return empty($config->disabled);
  2105. }
  2106. elseif ($op == 'delete') {
  2107. return ($config->export_type & EXPORT_IN_DATABASE) && !($config->export_type & EXPORT_IN_CODE);
  2108. }
  2109. elseif ($op == 'revert') {
  2110. return ($config->export_type & EXPORT_IN_DATABASE) && ($config->export_type & EXPORT_IN_CODE);
  2111. }
  2112. return FALSE;
  2113. }
  2114. /**
  2115. * Checks if a metatag configuration record is enabled.
  2116. *
  2117. * @param string $instance
  2118. * The configuration instance machine name.
  2119. *
  2120. * @return bool
  2121. * TRUE if the configuration is enabled, or FALSE otherwise.
  2122. */
  2123. function metatag_config_is_enabled($instance, $include_defaults = FALSE, $include_global = TRUE) {
  2124. if ($include_defaults) {
  2125. $instances = metatag_config_get_parent_instances($instance, $include_global);
  2126. $configs = metatag_config_load_multiple($instances);
  2127. // Check if one of the configs is enabled.
  2128. foreach ($configs as $config) {
  2129. if (empty($config->disabled)) {
  2130. return TRUE;
  2131. }
  2132. }
  2133. // No enabled configs found.
  2134. return FALSE;
  2135. }
  2136. else {
  2137. $config = metatag_config_load($instance);
  2138. return !empty($config) && empty($config->disabled);
  2139. }
  2140. }
  2141. /**
  2142. * Wrapper around entity_language().
  2143. *
  2144. * @param mixed $entity_type
  2145. * An entity type's machine name.
  2146. * @param object $entity
  2147. * The entity to review; will be typecast to an object if an array is passed.
  2148. *
  2149. * @return string
  2150. * A string indicating the language code to be used; returns LANGUAGE_NONE if
  2151. * the entity does not have a language assigned.
  2152. */
  2153. function metatag_entity_get_language($entity_type, $entity) {
  2154. // Determine the entity's language, af.
  2155. $langcode = entity_language($entity_type, (object) $entity);
  2156. // If no matching language was found, which will happen for e.g. terms and
  2157. // users, it is normally recommended to use the system default language.
  2158. // However, as the system default language can change, this could potentially
  2159. // cause data loss / confusion problems; as a result use the system "no
  2160. // language" value to avoid any potential problems.
  2161. if (empty($langcode)) {
  2162. $langcode = LANGUAGE_NONE;
  2163. }
  2164. return $langcode;
  2165. }
  2166. /**
  2167. * Implements hook_features_api().
  2168. */
  2169. function metatag_features_api() {
  2170. $components = array(
  2171. 'metatag' => array(
  2172. 'name' => t('Metatag'),
  2173. 'feature_source' => TRUE,
  2174. 'default_hook' => 'metatag_export_default',
  2175. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  2176. 'file' => drupal_get_path('module', 'metatag') . '/metatag.features.inc',
  2177. ),
  2178. );
  2179. return $components;
  2180. }
  2181. /**
  2182. * Implements hook_views_post_render().
  2183. *
  2184. * Try loading meta tags from a Views page display.
  2185. */
  2186. function metatag_views_post_render(&$view, &$output, &$cache) {
  2187. // By default do not add meta tags to admin pages. To enable meta tags on
  2188. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  2189. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  2190. return;
  2191. }
  2192. // If display is not set for some reason, get out to prevent PHP notices.
  2193. if (!isset($view->display[$view->current_display])) {
  2194. return;
  2195. }
  2196. // Build a shortcut to the current display object.
  2197. $display = $view->display[$view->current_display];
  2198. // Only proceed if this view is a full page and has a valid path, don't
  2199. // process block or other Views display objects.
  2200. if ($display->display_plugin == 'page' && !empty($display->display_options['path'])) {
  2201. // Try to work out what entity type this is.
  2202. $entity_type = '';
  2203. // All paths must have a placeholder.
  2204. $placeholder_pos = strpos($display->display_options['path'], '%');
  2205. if ($placeholder_pos !== FALSE) {
  2206. // The first argument must be a numeric entity ID.
  2207. if (!empty($view->args) && is_numeric($view->args[0])) {
  2208. // The first argument should be an entity ID.
  2209. $id = $view->args[0];
  2210. // Taxonomy terms are the most commonly used item, so check that first.
  2211. if ($display->display_options['path'] == 'taxonomy/term/%' || $display->display_options['path'] == 'taxonomy/term/%/%') {
  2212. $entity_type = 'taxonomy_term';
  2213. }
  2214. // Node pages.
  2215. elseif ($display->display_options['path'] == 'node/%') {
  2216. $entity_type = 'node';
  2217. }
  2218. // User pages.
  2219. elseif ($display->display_options['path'] == 'user/%') {
  2220. $entity_type = 'user';
  2221. }
  2222. }
  2223. }
  2224. // Check for other types of entities.
  2225. if (empty($entity_type)) {
  2226. // Trigger hook_metatag_views_post_render_get_entity().
  2227. $hook = 'metatag_views_post_render_get_entity';
  2228. foreach (module_implements($hook) as $module) {
  2229. $function = $module . '_' . $hook;
  2230. if (function_exists($function)) {
  2231. $entity_type = call_user_func($function, $view);
  2232. // If an entity type was identified, stop checking.
  2233. if (!empty($entity_type)) {
  2234. break;
  2235. }
  2236. }
  2237. }
  2238. }
  2239. // If an entity type was identified, try loading the entity.
  2240. if (!empty($entity_type)) {
  2241. // Try loading the requested entity.
  2242. $entities = entity_load($entity_type, array($id));
  2243. if (!empty($entities)) {
  2244. $entity = array_pop($entities);
  2245. // Trigger our hook_entity_view().
  2246. metatag_entity_view($entity, $entity_type, 'full', NULL, TRUE);
  2247. }
  2248. }
  2249. }
  2250. }
  2251. /**
  2252. * Implements hook_ctools_render_alter().
  2253. *
  2254. * Temporary solution to load meta tags on entity pages that are driven by
  2255. * CTools display handlers.
  2256. */
  2257. function metatag_ctools_render_alter(&$info, $page, $context) {
  2258. // By default do not add meta tags to admin pages. To enable meta tags on
  2259. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  2260. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  2261. return;
  2262. }
  2263. // Only proceed if this is a full page (don't process individual panes) and
  2264. // there's an 'admin path' for the current task.
  2265. if ($page && !empty($context['task']['admin path'])) {
  2266. // Loop over each context.
  2267. foreach ($context['contexts'] as $context_argument) {
  2268. if (is_array($context_argument->type) && !empty($context_argument->data)) {
  2269. if (in_array('entity', $context_argument->type)) {
  2270. $entity = $context_argument->data;
  2271. $entity_type = str_replace('entity:', '', $context_argument->plugin);
  2272. // Verify this is an appropriate entity.
  2273. $entity_info = entity_get_info($entity_type);
  2274. if (!empty($entity_info) && _metatag_entity_is_page($entity_type, $entity)) {
  2275. // Load the meta tags for this entity.
  2276. global $language;
  2277. metatag_entity_view($entity, $entity_type, 'full', $language->language, TRUE);
  2278. // Don't bother looping over any more contexts, an entity has been
  2279. // found.
  2280. break;
  2281. }
  2282. }
  2283. }
  2284. }
  2285. }
  2286. }
  2287. /**
  2288. * Checks if this entity is the default revision (published).
  2289. *
  2290. * Only needed when running Workbench Moderation v1; v3 is skipped.
  2291. *
  2292. * @param object $entity
  2293. * The entity object, e.g., $node.
  2294. *
  2295. * @return bool
  2296. * TRUE if the entity is the default revision, FALSE otherwise.
  2297. */
  2298. function _metatag_isdefaultrevision($entity) {
  2299. // D7 "Forward revisioning" is complex and causes a node_save() with the
  2300. // future node in node table. This fires hook_node_update() twice and cause
  2301. // abnormal behaviour in metatag.
  2302. //
  2303. // The steps taken by Workbench Moderation is to save the forward revision
  2304. // first and overwrite this with the live version in a shutdown function in
  2305. // a second step. This will confuse metatag. D7 has no generic property
  2306. // in the node object, if the node that is updated is the 'published' version
  2307. // or only a draft of a future version.
  2308. //
  2309. // This behaviour will change in D8 where $node->isDefaultRevision has been
  2310. // introduced. See below links for more details.
  2311. // - https://www.drupal.org/node/1879482
  2312. // - https://www.drupal.org/node/218755
  2313. // - https://www.drupal.org/node/1522154
  2314. //
  2315. // Every moderation module saving a forward revision needs to return FALSE.
  2316. // @todo: Refactor this workaround under D8.
  2317. // Workbench Moderation v1 uses the hook_node_presave() for some custom logic.
  2318. // This was replaced with hook_entity_presave() in v3, so only proceed if the
  2319. // old hook implementation is present.
  2320. if (function_exists('workbench_moderation_node_presave')) {
  2321. // If this is a node, check if the content type supports moderation.
  2322. if (function_exists('workbench_moderation_node_type_moderated') && workbench_moderation_node_type_moderated($entity->type) === TRUE) {
  2323. return !empty($entity->workbench_moderation['updating_live_revision']);
  2324. }
  2325. }
  2326. return FALSE;
  2327. }
  2328. /**
  2329. * Generate the cache ID to use with metatag_cache_get/metatag_cache_set calls.
  2330. *
  2331. * @param array $cid_parts
  2332. * A list of values to be used.
  2333. *
  2334. * @return string
  2335. * The cache ID string.
  2336. */
  2337. function metatag_cache_default_cid_parts(array $cid_parts = array()) {
  2338. // The initial parts, control the order of the parts.
  2339. $cid_part_defaults = array(
  2340. 'cache_type' => 'output',
  2341. 'instance' => '',
  2342. 'entity_type' => '',
  2343. 'entity_id' => '',
  2344. 'bundle' => '',
  2345. 'langcode' => $GLOBALS['language_content']->language,
  2346. 'revision_id' => '',
  2347. 'view_mode' => '',
  2348. 'status' => 200,
  2349. 'protocol' => $GLOBALS['is_https'] ? 'https' : 'http',
  2350. 'hostname' => $_SERVER['HTTP_HOST'],
  2351. 'base_path' => base_path(),
  2352. );
  2353. $cid_parts = array_merge($cid_part_defaults, $cid_parts);
  2354. // Add the HTTP status code.
  2355. $headers = drupal_get_http_header();
  2356. if (isset($headers['status'])) {
  2357. $cid_parts['status'] = intval($headers['status']);
  2358. }
  2359. // Allow each page in a sequence to have different values.
  2360. if (isset($_GET['page'])) {
  2361. $cid_parts['page'] = $_GET['page'];
  2362. }
  2363. // Allow other modules to customize the data using
  2364. // hook_metatag_page_cache_cid_parts_alter().
  2365. drupal_alter('metatag_page_cache_cid_parts', $cid_parts);
  2366. // Remove empty parts.
  2367. $cid_parts = array_filter($cid_parts);
  2368. // Concatenate the cache ID parts, trim the results to 255 chars.
  2369. return substr(implode(':', $cid_parts), 0, 255);
  2370. }
  2371. /**
  2372. * Wrapper for cache_set.
  2373. *
  2374. * @see cache_set()
  2375. */
  2376. function metatag_cache_set($cid, $data) {
  2377. // By default the cached data will not expire.
  2378. $expire = CACHE_PERMANENT;
  2379. // Triggers hook_metatag_cache_set_expire_alter().
  2380. drupal_alter("metatag_cache_set_expire", $expire, $cid, $data);
  2381. return cache_set($cid, $data, 'cache_metatag', $expire);
  2382. }
  2383. /**
  2384. * Wrapper for cache_get.
  2385. *
  2386. * @see cache_get()
  2387. */
  2388. function metatag_cache_get($cid) {
  2389. // Try to load the object.
  2390. return cache_get($cid, 'cache_metatag');
  2391. }
  2392. /**
  2393. * Determines if we are in an error page and return the appropriate instance.
  2394. *
  2395. * @return string
  2396. * String of error.
  2397. */
  2398. function metatag_is_error_page() {
  2399. $known_errors = array(
  2400. 'global:403' => '403 Forbidden',
  2401. 'global:404' => '404 Not Found',
  2402. );
  2403. $headers = drupal_get_http_header();
  2404. if (isset($headers['status'])) {
  2405. foreach ($known_errors as $error => $status) {
  2406. if ($status == $headers['status']) {
  2407. return $error;
  2408. }
  2409. }
  2410. }
  2411. return '';
  2412. }
  2413. /**
  2414. * Implements hook_admin_menu_cache_info().
  2415. */
  2416. function metatag_admin_menu_cache_info() {
  2417. $caches['metatag'] = array(
  2418. 'title' => t('Metatag'),
  2419. 'callback' => 'metatag_config_cache_clear',
  2420. );
  2421. return $caches;
  2422. }
  2423. /**
  2424. * Identify whether an entity type is technically capable of having meta tags.
  2425. *
  2426. * In order to be capable of having meta tags, an entity type must have view
  2427. * modes, must be fieldable, and may not be a configuration entity.
  2428. *
  2429. * @param string $entity_type
  2430. * The entity type.
  2431. * @param array $entity_info
  2432. * Entity information.
  2433. *
  2434. * @return bool
  2435. * Return TRUE if suitable.
  2436. */
  2437. function metatag_entity_type_is_suitable($entity_type, $entity_info = array()) {
  2438. $suitable = TRUE;
  2439. // If the entity info was not passed along, load it.
  2440. if (empty($entity_info)) {
  2441. $entity_info = entity_get_info($entity_type);
  2442. }
  2443. // Configuration entities may not have meta tags.
  2444. if (isset($entity_info['configuration']) && $entity_info['configuration'] == TRUE) {
  2445. $suitable = FALSE;
  2446. }
  2447. // Entities must have bundles.
  2448. elseif (empty($entity_info['bundles'])) {
  2449. $suitable = FALSE;
  2450. }
  2451. // The entity type must be fieldable.
  2452. elseif (empty($entity_info['fieldable'])) {
  2453. $suitable = FALSE;
  2454. }
  2455. else {
  2456. // Ignore some view modes that are automatically added by certain modules.
  2457. unset($entity_info['view modes']['ical']);
  2458. unset($entity_info['view modes']['diff_standard']);
  2459. unset($entity_info['view modes']['token']);
  2460. // There must be view modes.
  2461. if (empty($entity_info['view modes'])) {
  2462. $suitable = FALSE;
  2463. }
  2464. else {
  2465. // Specifically disable some entity types.
  2466. $excluded = array(
  2467. // Comment module.
  2468. 'comment',
  2469. // Field Collection module.
  2470. 'field_collection_item',
  2471. // Paragraphs module.
  2472. 'paragraphs_item',
  2473. );
  2474. if (in_array($entity_type, $excluded)) {
  2475. $suitable = FALSE;
  2476. }
  2477. }
  2478. }
  2479. // Trigger hook_metatag_entity_type_is_supported_alter() to allow other
  2480. // modules to either enable or disable certain entity types.
  2481. drupal_alter('metatag_entity_type_is_supported', $suitable, $entity_type, $entity_info);
  2482. return $suitable;
  2483. }
  2484. /**
  2485. * Implements hook_node_type_insert().
  2486. *
  2487. * When a content type is created, enable it for use with Metatag.
  2488. */
  2489. function metatag_node_type_insert($info) {
  2490. if (metatag_entity_supports_metatags('node')) {
  2491. if (metatag_entity_type_enable('node', $info->type)) {
  2492. drupal_set_message(t('Metatag support has been enabled for the @label content type.', array('@label' => $info->name)));
  2493. }
  2494. }
  2495. }
  2496. /**
  2497. * Implements hook_node_type_delete().
  2498. *
  2499. * When a content type is deleted, remove the corresponding Metatag variable.
  2500. */
  2501. function metatag_node_type_delete($info) {
  2502. variable_del('metatag_enable_node__' . $info->type);
  2503. }
  2504. /**
  2505. * Implements hook_taxonomy_vocabulary_insert().
  2506. *
  2507. * When a vocabulary is created, enable it for use with Metatag.
  2508. */
  2509. function metatag_taxonomy_vocabulary_insert($vocabulary) {
  2510. if (metatag_entity_supports_metatags('taxonomy_term')) {
  2511. if (metatag_entity_type_enable('taxonomy_term', $vocabulary->machine_name)) {
  2512. drupal_set_message(t('Metatag support has been enabled for the @label vocabulary.', array('@label' => $vocabulary->name)));
  2513. }
  2514. }
  2515. }
  2516. /**
  2517. * Implements hook_taxonomy_vocabulary_delete().
  2518. *
  2519. * When a vocabulary is deleted, remove the corresponding Metatag variable.
  2520. */
  2521. function metatag_taxonomy_vocabulary_delete($info) {
  2522. variable_del('metatag_enable_taxonomy_term__' . $info->machine_name);
  2523. }
  2524. /**
  2525. * Implements hook_workbench_moderation_transition().
  2526. *
  2527. * Clear a node's caches when its Workbench Moderation state is changed.
  2528. */
  2529. function metatag_workbench_moderation_transition($node, $previous_state, $new_state) {
  2530. metatag_metatags_cache_clear('node', array($node->nid));
  2531. }
  2532. /**
  2533. * Sort callback for sorting by metatag instance string values.
  2534. */
  2535. function _metatag_config_instance_sort($a, $b) {
  2536. $a_contexts = explode(':', $a);
  2537. $b_contexts = explode(':', $b);
  2538. // Global config always comes first.
  2539. if ($a_contexts[0] == 'global' && $b_contexts[0] != 'global') {
  2540. return -1;
  2541. }
  2542. elseif ($b_contexts[0] == 'global' && $a_contexts[0] != 'global') {
  2543. return 1;
  2544. }
  2545. for ($i = 0; $i < max(count($a_contexts), count($b_contexts)); $i++) {
  2546. $a_context = isset($a_contexts[$i]) ? $a_contexts[$i] : '';
  2547. $b_context = isset($b_contexts[$i]) ? $b_contexts[$i] : '';
  2548. if ($a_context == $b_context) {
  2549. continue;
  2550. }
  2551. else {
  2552. return strcmp($a_context, $b_context);
  2553. }
  2554. }
  2555. }
  2556. /**
  2557. * Translations & internationalization (i18n).
  2558. */
  2559. /**
  2560. * Implements hook_entity_translation_delete().
  2561. *
  2562. * Required for content translations being handled via Entity_Translation to
  2563. * remove the appropriate record when a translation is removed without the
  2564. * corresponding entity record also being removed.
  2565. */
  2566. function metatag_entity_translation_delete($entity_type, $entity, $langcode) {
  2567. // Get the entity's ID.
  2568. list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
  2569. $revision_id = intval($revision_id);
  2570. // Delete the translation.
  2571. metatag_metatags_delete($entity_type, $entity_id, $revision_id, $langcode);
  2572. }
  2573. /**
  2574. * Translates the metatag if i18n_string integration is enabled.
  2575. *
  2576. * @param string $string
  2577. * String in default language or array of strings to be translated.
  2578. * @param string $tag_name
  2579. * The internal name of the meta tag being translated.
  2580. * @param mixed $context
  2581. * A context string to use with i18n, or the $options array from a Metatag::
  2582. * getValue() method; if the latter it will be used to generate the full
  2583. * context.
  2584. * @param string $langcode
  2585. * The language code to submit instead of the current page's language.
  2586. * @param bool $update
  2587. * Whether or not to create/update records in {locales_source}.
  2588. *
  2589. * @return string
  2590. * The translated string if i18n_string is enabled, otherwise just returns the
  2591. * original string.
  2592. *
  2593. * @see i18n_string_translate()
  2594. */
  2595. function metatag_translate_metatag($string, $tag_name, $context, $langcode = NULL, $update = TRUE) {
  2596. if (module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
  2597. // By default do not add meta tags to admin pages. To enable meta tags on
  2598. // admin pages set the 'metatag_tag_admin_pages' variable to TRUE.
  2599. static $page_is_admin;
  2600. if (is_null($page_is_admin)) {
  2601. $page_is_admin = FALSE;
  2602. if (path_is_admin(current_path()) && !variable_get('metatag_tag_admin_pages', FALSE)) {
  2603. $page_is_admin = TRUE;
  2604. }
  2605. }
  2606. if ($page_is_admin) {
  2607. return $string;
  2608. }
  2609. // If the context is an array then it is the $options from the meta tag
  2610. // generator and needs some custom tailoring. Doing it this way to avoid an
  2611. // unnecessary entity_extract_ids() call when i18n isn't being used.
  2612. if (is_array($context)) {
  2613. // Optionally disable output generation.
  2614. if (!variable_get('metatag_i18n_translate_output', FALSE)) {
  2615. return $string;
  2616. }
  2617. // Output generation was enabled, so continue as normal.
  2618. $new_context = 'output:';
  2619. if (drupal_is_front_page()) {
  2620. $new_context .= 'frontpage';
  2621. }
  2622. // If this is an entity page, use the entity as the context.
  2623. elseif (!empty($context['entity_type']) && !empty($context['entity'])) {
  2624. list($entity_id, $revision_id, $bundle) = entity_extract_ids($context['entity_type'], $context['entity']);
  2625. $new_context .= $context['entity_type'] . ':' . $entity_id;
  2626. }
  2627. // Otherwise, use the page URL.
  2628. else {
  2629. // Trim this to avoid SQL errors on the {locales_source} table.
  2630. // length = 255 - strlen('metatag:output:page:') - strlen(metatag);
  2631. $strlen = 255 - strlen('metatag:output:page:' . $tag_name);
  2632. $new_context .= 'page:' . drupal_substr(current_path(), 0, $strlen);
  2633. }
  2634. $context = $new_context;
  2635. }
  2636. $options = array(
  2637. // Automatically create/update the {locales_source} record if one wasn't
  2638. // found.
  2639. 'update' => $update,
  2640. // Translate the string.
  2641. 'translate' => TRUE,
  2642. );
  2643. // If the langcode was passed in, add it to the options passed to i18n.
  2644. if (!empty($langcode)) {
  2645. $options['langcode'] = $langcode;
  2646. }
  2647. // By default disable the watchdog logging of translation messages.
  2648. $options['watchdog'] = variable_get('metatag_i18n_enable_watchdog', FALSE);
  2649. // Triggers hook_metatag_i18n_context_alter() - allows the i18n string to
  2650. // be altered before being used.
  2651. drupal_alter('metatag_i18n_context', $context, $tag_name);
  2652. // If the context was erased just send back the original string - it's
  2653. // unlikely, but it could happen.
  2654. if (empty($context)) {
  2655. return $string;
  2656. }
  2657. // The 'name' is split up by i18n_string into two components - the textgroup
  2658. // is the first item, the others are joined together with a ':' separator
  2659. // to make the context. In order to have the contexts show with "metatag" as
  2660. // the first part of the context, it has to be added twice to the name.
  2661. $name = array(
  2662. 'metatag',
  2663. $context,
  2664. $tag_name,
  2665. );
  2666. // Notify i18n of the string, and obtain a translation if one is available.
  2667. return i18n_string($name, $string, $options);
  2668. }
  2669. // If the i18n_string module isn't enabled then just pass back the string
  2670. // as-is.
  2671. else {
  2672. return $string;
  2673. }
  2674. }
  2675. /**
  2676. * Translate a set of metatags to the current language.
  2677. *
  2678. * @param array $metatags
  2679. * List of meta tags to be translated.
  2680. * @param string $context
  2681. * An optional context to use with i18n.
  2682. * @param string $langcode
  2683. * The language code to submit instead of the current page's language.
  2684. * @param bool $update
  2685. * Whether or not to create/update records in {locales_source}.
  2686. */
  2687. function metatag_translate_metatags(&$metatags, $context = NULL, $langcode = NULL, $update = TRUE) {
  2688. if (!empty($metatags)) {
  2689. foreach ($metatags as $key => $data) {
  2690. if (!empty($data['value']) && is_string($data['value'])) {
  2691. $metatags[$key] = array(
  2692. 'value' => metatag_translate_metatag($data['value'], $key, $context, $langcode, $update),
  2693. );
  2694. }
  2695. }
  2696. }
  2697. }
  2698. /**
  2699. * Update the translated definitions of meta tags.
  2700. *
  2701. * @param array $metatags
  2702. * List of meta tags to have their translations updated.
  2703. * @param string $context
  2704. * The string that will be used to group strings in the translation UI.
  2705. */
  2706. function metatag_translations_update($metatags, $context) {
  2707. // Store the context as it was originally provided.
  2708. $original_context = $context;
  2709. // Update the i18n string.
  2710. if (module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
  2711. $options = array();
  2712. // By default disable the watchdog logging of translation messages.
  2713. $options['watchdog'] = variable_get('metatag_i18n_enable_watchdog', FALSE);
  2714. foreach ($metatags as $tag_name => $data) {
  2715. // Revert the context, so that it can be changed if needed.
  2716. $context = $original_context;
  2717. if (!empty($data['value']) && is_string($data['value'])) {
  2718. // Triggers hook_metatag_i18n_context_alter() - allows the i18n string
  2719. // to be altered before being used.
  2720. drupal_alter('metatag_i18n_context', $context, $tag_name);
  2721. // Don't do anything if the context was erased - it's unlikely, but it
  2722. // could happen.
  2723. if (empty($context)) {
  2724. continue;
  2725. }
  2726. // The textgroup is the first part of the string.
  2727. i18n_string_update("metatag:{$context}:{$tag_name}", $data['value'], $options);
  2728. }
  2729. }
  2730. }
  2731. }
  2732. /**
  2733. * Remove the translated definitions of meta tags.
  2734. *
  2735. * @param array $metatags
  2736. * List of meta tags to have their translations updated.
  2737. * @param string $context
  2738. * The string that will be used to group strings in the translation UI.
  2739. */
  2740. function metatag_translations_delete($metatags, $context) {
  2741. // Store the context as it was originally provided.
  2742. $original_context = $context;
  2743. // Update the i18n string.
  2744. if (module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
  2745. $options = array();
  2746. // By default disable the watchdog logging of translation messages.
  2747. $options['watchdog'] = variable_get('metatag_i18n_enable_watchdog', FALSE);
  2748. foreach ($metatags as $tag_name => $data) {
  2749. // Revert the context, so that it can be changed if needed.
  2750. $context = $original_context;
  2751. if (!empty($data['value']) && is_string($data['value'])) {
  2752. // Triggers hook_metatag_i18n_context_alter() - allows the i18n string
  2753. // to be altered before being used.
  2754. drupal_alter('metatag_i18n_context', $context, $tag_name);
  2755. // Don't do anything if the context was erased - it's unlikely, but it
  2756. // could happen.
  2757. if (empty($context)) {
  2758. continue;
  2759. }
  2760. // The textgroup is the first part of the string.
  2761. i18n_string_remove("metatag:{$context}:{$tag_name}", $data['value'], $options);
  2762. }
  2763. }
  2764. }
  2765. }
  2766. /**
  2767. * Implements hook_config_insert().
  2768. *
  2769. * Implements hook_metatag_config_insert() on behalf of i18n_string.
  2770. */
  2771. function i18n_string_metatag_config_insert($config) {
  2772. $context = 'metatag_config:' . $config->instance;
  2773. metatag_translations_update($config->config, $context);
  2774. }
  2775. /**
  2776. * Implements hook_config_update().
  2777. *
  2778. * Implements hook_metatag_config_update() on behalf of i18n_string.
  2779. */
  2780. function i18n_string_metatag_config_update($config) {
  2781. // Defer to the 'insert' function.
  2782. i18n_string_metatag_config_insert($config);
  2783. }
  2784. /**
  2785. * Implements hook_config_delete().
  2786. *
  2787. * Implements hook_metatag_config_delete() on behalf of i18n_string.
  2788. */
  2789. function i18n_string_metatag_config_delete($config) {
  2790. $context = 'metatag_config:' . $config->instance;
  2791. metatag_translations_delete($config->config, $context);
  2792. }