metatag.module 101 KB

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