system.api.php 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by Drupal core and the System module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Defines one or more hooks that are exposed by a module.
  12. *
  13. * Normally hooks do not need to be explicitly defined. However, by declaring a
  14. * hook explicitly, a module may define a "group" for it. Modules that implement
  15. * a hook may then place their implementation in either $module.module or in
  16. * $module.$group.inc. If the hook is located in $module.$group.inc, then that
  17. * file will be automatically loaded when needed.
  18. * In general, hooks that are rarely invoked and/or are very large should be
  19. * placed in a separate include file, while hooks that are very short or very
  20. * frequently called should be left in the main module file so that they are
  21. * always available.
  22. *
  23. * @return
  24. * An associative array whose keys are hook names and whose values are an
  25. * associative array containing:
  26. * - group: A string defining the group to which the hook belongs. The module
  27. * system will determine whether a file with the name $module.$group.inc
  28. * exists, and automatically load it when required.
  29. *
  30. * See system_hook_info() for all hook groups defined by Drupal core.
  31. *
  32. * @see hook_hook_info_alter().
  33. */
  34. function hook_hook_info() {
  35. $hooks['token_info'] = array(
  36. 'group' => 'tokens',
  37. );
  38. $hooks['tokens'] = array(
  39. 'group' => 'tokens',
  40. );
  41. return $hooks;
  42. }
  43. /**
  44. * Alter information from hook_hook_info().
  45. *
  46. * @param $hooks
  47. * Information gathered by module_hook_info() from other modules'
  48. * implementations of hook_hook_info(). Alter this array directly.
  49. * See hook_hook_info() for information on what this may contain.
  50. */
  51. function hook_hook_info_alter(&$hooks) {
  52. // Our module wants to completely override the core tokens, so make
  53. // sure the core token hooks are not found.
  54. $hooks['token_info']['group'] = 'mytokens';
  55. $hooks['tokens']['group'] = 'mytokens';
  56. }
  57. /**
  58. * Inform the base system and the Field API about one or more entity types.
  59. *
  60. * Inform the system about one or more entity types (i.e., object types that
  61. * can be loaded via entity_load() and, optionally, to which fields can be
  62. * attached).
  63. *
  64. * @return
  65. * An array whose keys are entity type names and whose values identify
  66. * properties of those types that the system needs to know about:
  67. * - label: The human-readable name of the type.
  68. * - controller class: The name of the class that is used to load the objects.
  69. * The class has to implement the DrupalEntityControllerInterface interface.
  70. * Leave blank to use the DrupalDefaultEntityController implementation.
  71. * - base table: (used by DrupalDefaultEntityController) The name of the
  72. * entity type's base table.
  73. * - revision table: The name of the entity type's revision table (if any).
  74. * - static cache: (used by DrupalDefaultEntityController) FALSE to disable
  75. * static caching of entities during a page request. Defaults to TRUE.
  76. * - field cache: (used by Field API loading and saving of field data) FALSE
  77. * to disable Field API's persistent cache of field data. Only recommended
  78. * if a higher level persistent cache is available for the entity type.
  79. * Defaults to TRUE.
  80. * - load hook: The name of the hook which should be invoked by
  81. * DrupalDefaultEntityController:attachLoad(), for example 'node_load'.
  82. * - uri callback: A function taking an entity as argument and returning the
  83. * URI elements of the entity, e.g. 'path' and 'options'. The actual entity
  84. * URI can be constructed by passing these elements to url().
  85. * - label callback: (optional) A function taking an entity and an entity type
  86. * as arguments and returning the label of the entity. The entity label is
  87. * the main string associated with an entity; for example, the title of a
  88. * node or the subject of a comment. If there is an entity object property
  89. * that defines the label, use the 'label' element of the 'entity keys'
  90. * return value component to provide this information (see below). If more
  91. * complex logic is needed to determine the label of an entity, you can
  92. * instead specify a callback function here, which will be called to
  93. * determine the entity label. See also the entity_label() function, which
  94. * implements this logic.
  95. * - language callback: (optional) A function taking an entity and an entity
  96. * type as arguments and returning a language code. In most situations, when
  97. * needing to determine this value, inspecting a property named after the
  98. * 'language' element of the 'entity keys' should be enough. The language
  99. * callback is meant to be used primarily for temporary alterations of the
  100. * property value: entity-defining modules are encouraged to always define a
  101. * language property, instead of using the callback as main entity language
  102. * source. In fact not having a language property defined is likely to
  103. * prevent an entity from being queried by language. Moreover, given that
  104. * entity_language() is not necessarily used everywhere it would be
  105. * appropriate, modules implementing the language callback should be aware
  106. * that this might not be always called.
  107. * - fieldable: Set to TRUE if you want your entity type to accept fields
  108. * being attached to it.
  109. * - translation: An associative array of modules registered as field
  110. * translation handlers. Array keys are the module names, array values
  111. * can be any data structure the module uses to provide field translation.
  112. * Any empty value disallows the module to appear as a translation handler.
  113. * - entity keys: An array describing how the Field API can extract the
  114. * information it needs from the objects of the type. Elements:
  115. * - id: The name of the property that contains the primary id of the
  116. * entity. Every entity object passed to the Field API must have this
  117. * property and its value must be numeric.
  118. * - revision: The name of the property that contains the revision id of
  119. * the entity. The Field API assumes that all revision ids are unique
  120. * across all entities of a type. This entry can be omitted if the
  121. * entities of this type are not versionable.
  122. * - bundle: The name of the property that contains the bundle name for the
  123. * entity. The bundle name defines which set of fields are attached to
  124. * the entity (e.g. what nodes call "content type"). This entry can be
  125. * omitted if this entity type exposes a single bundle (all entities have
  126. * the same collection of fields). The name of this single bundle will be
  127. * the same as the entity type.
  128. * - label: The name of the property that contains the entity label. For
  129. * example, if the entity's label is located in $entity->subject, then
  130. * 'subject' should be specified here. If complex logic is required to
  131. * build the label, a 'label callback' should be defined instead (see
  132. * the 'label callback' section above for details).
  133. * - language: The name of the property, typically 'language', that contains
  134. * the language code representing the language the entity has been created
  135. * in. This value may be changed when editing the entity and represents
  136. * the language its textual components are supposed to have. If no
  137. * language property is available, the 'language callback' may be used
  138. * instead. This entry can be omitted if the entities of this type are not
  139. * language-aware.
  140. * - bundle keys: An array describing how the Field API can extract the
  141. * information it needs from the bundle objects for this type. This entry
  142. * is required if the 'path' provided in the 'bundles'/'admin' section
  143. * identifies the bundle using a named menu placeholder whose loader
  144. * callback returns an object (e.g., $vocabulary for taxonomy terms, or
  145. * $node_type for nodes). If the path does not include the bundle, or the
  146. * bundle is just a string rather than an automatically loaded object, then
  147. * this can be omitted. Elements:
  148. * - bundle: The name of the property of the bundle object that contains
  149. * the name of the bundle object.
  150. * - bundles: An array describing all bundles for this object type. Keys are
  151. * bundles machine names, as found in the objects' 'bundle' property
  152. * (defined in the 'entity keys' entry above). This entry can be omitted if
  153. * this entity type exposes a single bundle (all entities have the same
  154. * collection of fields). The name of this single bundle will be the same as
  155. * the entity type. Elements:
  156. * - label: The human-readable name of the bundle.
  157. * - uri callback: Same as the 'uri callback' key documented above for the
  158. * entity type, but for the bundle only. When determining the URI of an
  159. * entity, if a 'uri callback' is defined for both the entity type and
  160. * the bundle, the one for the bundle is used.
  161. * - admin: An array of information that allows Field UI pages to attach
  162. * themselves to the existing administration pages for the bundle.
  163. * Elements:
  164. * - path: the path of the bundle's main administration page, as defined
  165. * in hook_menu(). If the path includes a placeholder for the bundle,
  166. * the 'bundle argument' and 'real path' keys below are required.
  167. * - bundle argument: The position of the bundle placeholder in 'path', if
  168. * any.
  169. * - real path: The actual path (no placeholder) of the bundle's main
  170. * administration page. This will be used to generate links.
  171. * - access callback: As in hook_menu(). 'user_access' will be assumed if
  172. * no value is provided.
  173. * - access arguments: As in hook_menu().
  174. * - view modes: An array describing the view modes for the entity type. View
  175. * modes let entities be displayed differently depending on the context.
  176. * For instance, a node can be displayed differently on its own page
  177. * ('full' mode), on the home page or taxonomy listings ('teaser' mode), or
  178. * in an RSS feed ('rss' mode). Modules taking part in the display of the
  179. * entity (notably the Field API) can adjust their behavior depending on
  180. * the requested view mode. An additional 'default' view mode is available
  181. * for all entity types. This view mode is not intended for actual entity
  182. * display, but holds default display settings. For each available view
  183. * mode, administrators can configure whether it should use its own set of
  184. * field display settings, or just replicate the settings of the 'default'
  185. * view mode, thus reducing the amount of display configurations to keep
  186. * track of. Keys of the array are view mode names. Each view mode is
  187. * described by an array with the following key/value pairs:
  188. * - label: The human-readable name of the view mode
  189. * - custom settings: A boolean specifying whether the view mode should by
  190. * default use its own custom field display settings. If FALSE, entities
  191. * displayed in this view mode will reuse the 'default' display settings
  192. * by default (e.g. right after the module exposing the view mode is
  193. * enabled), but administrators can later use the Field UI to apply custom
  194. * display settings specific to the view mode.
  195. *
  196. * @see entity_load()
  197. * @see hook_entity_info_alter()
  198. */
  199. function hook_entity_info() {
  200. $return = array(
  201. 'node' => array(
  202. 'label' => t('Node'),
  203. 'controller class' => 'NodeController',
  204. 'base table' => 'node',
  205. 'revision table' => 'node_revision',
  206. 'uri callback' => 'node_uri',
  207. 'fieldable' => TRUE,
  208. 'translation' => array(
  209. 'locale' => TRUE,
  210. ),
  211. 'entity keys' => array(
  212. 'id' => 'nid',
  213. 'revision' => 'vid',
  214. 'bundle' => 'type',
  215. 'language' => 'language',
  216. ),
  217. 'bundle keys' => array(
  218. 'bundle' => 'type',
  219. ),
  220. 'bundles' => array(),
  221. 'view modes' => array(
  222. 'full' => array(
  223. 'label' => t('Full content'),
  224. 'custom settings' => FALSE,
  225. ),
  226. 'teaser' => array(
  227. 'label' => t('Teaser'),
  228. 'custom settings' => TRUE,
  229. ),
  230. 'rss' => array(
  231. 'label' => t('RSS'),
  232. 'custom settings' => FALSE,
  233. ),
  234. ),
  235. ),
  236. );
  237. // Search integration is provided by node.module, so search-related
  238. // view modes for nodes are defined here and not in search.module.
  239. if (module_exists('search')) {
  240. $return['node']['view modes'] += array(
  241. 'search_index' => array(
  242. 'label' => t('Search index'),
  243. 'custom settings' => FALSE,
  244. ),
  245. 'search_result' => array(
  246. 'label' => t('Search result'),
  247. 'custom settings' => FALSE,
  248. ),
  249. );
  250. }
  251. // Bundles must provide a human readable name so we can create help and error
  252. // messages, and the path to attach Field admin pages to.
  253. foreach (node_type_get_names() as $type => $name) {
  254. $return['node']['bundles'][$type] = array(
  255. 'label' => $name,
  256. 'admin' => array(
  257. 'path' => 'admin/structure/types/manage/%node_type',
  258. 'real path' => 'admin/structure/types/manage/' . str_replace('_', '-', $type),
  259. 'bundle argument' => 4,
  260. 'access arguments' => array('administer content types'),
  261. ),
  262. );
  263. }
  264. return $return;
  265. }
  266. /**
  267. * Alter the entity info.
  268. *
  269. * Modules may implement this hook to alter the information that defines an
  270. * entity. All properties that are available in hook_entity_info() can be
  271. * altered here.
  272. *
  273. * @param $entity_info
  274. * The entity info array, keyed by entity name.
  275. *
  276. * @see hook_entity_info()
  277. */
  278. function hook_entity_info_alter(&$entity_info) {
  279. // Set the controller class for nodes to an alternate implementation of the
  280. // DrupalEntityController interface.
  281. $entity_info['node']['controller class'] = 'MyCustomNodeController';
  282. }
  283. /**
  284. * Act on entities when loaded.
  285. *
  286. * This is a generic load hook called for all entity types loaded via the
  287. * entity API.
  288. *
  289. * @param $entities
  290. * The entities keyed by entity ID.
  291. * @param $type
  292. * The type of entities being loaded (i.e. node, user, comment).
  293. */
  294. function hook_entity_load($entities, $type) {
  295. foreach ($entities as $entity) {
  296. $entity->foo = mymodule_add_something($entity, $type);
  297. }
  298. }
  299. /**
  300. * Act on an entity before it is about to be created or updated.
  301. *
  302. * @param $entity
  303. * The entity object.
  304. * @param $type
  305. * The type of entity being saved (i.e. node, user, comment).
  306. */
  307. function hook_entity_presave($entity, $type) {
  308. $entity->changed = REQUEST_TIME;
  309. }
  310. /**
  311. * Act on entities when inserted.
  312. *
  313. * @param $entity
  314. * The entity object.
  315. * @param $type
  316. * The type of entity being inserted (i.e. node, user, comment).
  317. */
  318. function hook_entity_insert($entity, $type) {
  319. // Insert the new entity into a fictional table of all entities.
  320. $info = entity_get_info($type);
  321. list($id) = entity_extract_ids($type, $entity);
  322. db_insert('example_entity')
  323. ->fields(array(
  324. 'type' => $type,
  325. 'id' => $id,
  326. 'created' => REQUEST_TIME,
  327. 'updated' => REQUEST_TIME,
  328. ))
  329. ->execute();
  330. }
  331. /**
  332. * Act on entities when updated.
  333. *
  334. * @param $entity
  335. * The entity object.
  336. * @param $type
  337. * The type of entity being updated (i.e. node, user, comment).
  338. */
  339. function hook_entity_update($entity, $type) {
  340. // Update the entity's entry in a fictional table of all entities.
  341. $info = entity_get_info($type);
  342. list($id) = entity_extract_ids($type, $entity);
  343. db_update('example_entity')
  344. ->fields(array(
  345. 'updated' => REQUEST_TIME,
  346. ))
  347. ->condition('type', $type)
  348. ->condition('id', $id)
  349. ->execute();
  350. }
  351. /**
  352. * Act on entities when deleted.
  353. *
  354. * @param $entity
  355. * The entity object.
  356. * @param $type
  357. * The type of entity being deleted (i.e. node, user, comment).
  358. */
  359. function hook_entity_delete($entity, $type) {
  360. // Delete the entity's entry from a fictional table of all entities.
  361. $info = entity_get_info($type);
  362. list($id) = entity_extract_ids($type, $entity);
  363. db_delete('example_entity')
  364. ->condition('type', $type)
  365. ->condition('id', $id)
  366. ->execute();
  367. }
  368. /**
  369. * Alter or execute an EntityFieldQuery.
  370. *
  371. * @param EntityFieldQuery $query
  372. * An EntityFieldQuery. One of the most important properties to be changed is
  373. * EntityFieldQuery::executeCallback. If this is set to an existing function,
  374. * this function will get the query as its single argument and its result
  375. * will be the returned as the result of EntityFieldQuery::execute(). This can
  376. * be used to change the behavior of EntityFieldQuery entirely. For example,
  377. * the default implementation can only deal with one field storage engine, but
  378. * it is possible to write a module that can query across field storage
  379. * engines. Also, the default implementation presumes entities are stored in
  380. * SQL, but the execute callback could instead query any other entity storage,
  381. * local or remote.
  382. *
  383. * Note the $query->altered attribute which is TRUE in case the query has
  384. * already been altered once. This happens with cloned queries.
  385. * If there is a pager, then such a cloned query will be executed to count
  386. * all elements. This query can be detected by checking for
  387. * ($query->pager && $query->count), allowing the driver to return 0 from
  388. * the count query and disable the pager.
  389. */
  390. function hook_entity_query_alter($query) {
  391. $query->executeCallback = 'my_module_query_callback';
  392. }
  393. /**
  394. * Act on entities being assembled before rendering.
  395. *
  396. * @param $entity
  397. * The entity object.
  398. * @param $type
  399. * The type of entity being rendered (i.e. node, user, comment).
  400. * @param $view_mode
  401. * The view mode the entity is rendered in.
  402. * @param $langcode
  403. * The language code used for rendering.
  404. *
  405. * The module may add elements to $entity->content prior to rendering. The
  406. * structure of $entity->content is a renderable array as expected by
  407. * drupal_render().
  408. *
  409. * @see hook_entity_view_alter()
  410. * @see hook_comment_view()
  411. * @see hook_node_view()
  412. * @see hook_user_view()
  413. */
  414. function hook_entity_view($entity, $type, $view_mode, $langcode) {
  415. $entity->content['my_additional_field'] = array(
  416. '#markup' => $additional_field,
  417. '#weight' => 10,
  418. '#theme' => 'mymodule_my_additional_field',
  419. );
  420. }
  421. /**
  422. * Alter the results of ENTITY_view().
  423. *
  424. * This hook is called after the content has been assembled in a structured
  425. * array and may be used for doing processing which requires that the complete
  426. * entity content structure has been built.
  427. *
  428. * If a module wishes to act on the rendered HTML of the entity rather than the
  429. * structured content array, it may use this hook to add a #post_render
  430. * callback. Alternatively, it could also implement hook_preprocess_ENTITY().
  431. * See drupal_render() and theme() for details.
  432. *
  433. * @param $build
  434. * A renderable array representing the entity content.
  435. * @param $type
  436. * The type of entity being rendered (i.e. node, user, comment).
  437. *
  438. * @see hook_entity_view()
  439. * @see hook_comment_view_alter()
  440. * @see hook_node_view_alter()
  441. * @see hook_taxonomy_term_view_alter()
  442. * @see hook_user_view_alter()
  443. */
  444. function hook_entity_view_alter(&$build, $type) {
  445. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  446. // Change its weight.
  447. $build['an_additional_field']['#weight'] = -10;
  448. // Add a #post_render callback to act on the rendered HTML of the entity.
  449. $build['#post_render'][] = 'my_module_node_post_render';
  450. }
  451. }
  452. /**
  453. * Change the view mode of an entity that is being displayed.
  454. *
  455. * @param string $view_mode
  456. * The view_mode that is to be used to display the entity.
  457. * @param array $context
  458. * Array with contextual information, including:
  459. * - entity_type: The type of the entity that is being viewed.
  460. * - entity: The entity object.
  461. * - langcode: The langcode the entity is being viewed in.
  462. */
  463. function hook_entity_view_mode_alter(&$view_mode, $context) {
  464. // For nodes, change the view mode when it is teaser.
  465. if ($context['entity_type'] == 'node' && $view_mode == 'teaser') {
  466. $view_mode = 'my_custom_view_mode';
  467. }
  468. }
  469. /**
  470. * Define administrative paths.
  471. *
  472. * Modules may specify whether or not the paths they define in hook_menu() are
  473. * to be considered administrative. Other modules may use this information to
  474. * display those pages differently (e.g. in a modal overlay, or in a different
  475. * theme).
  476. *
  477. * To change the administrative status of menu items defined in another module's
  478. * hook_menu(), modules should implement hook_admin_paths_alter().
  479. *
  480. * @return
  481. * An associative array. For each item, the key is the path in question, in
  482. * a format acceptable to drupal_match_path(). The value for each item should
  483. * be TRUE (for paths considered administrative) or FALSE (for non-
  484. * administrative paths).
  485. *
  486. * @see hook_menu()
  487. * @see drupal_match_path()
  488. * @see hook_admin_paths_alter()
  489. */
  490. function hook_admin_paths() {
  491. $paths = array(
  492. 'mymodule/*/add' => TRUE,
  493. 'mymodule/*/edit' => TRUE,
  494. );
  495. return $paths;
  496. }
  497. /**
  498. * Redefine administrative paths defined by other modules.
  499. *
  500. * @param $paths
  501. * An associative array of administrative paths, as defined by implementations
  502. * of hook_admin_paths().
  503. *
  504. * @see hook_admin_paths()
  505. */
  506. function hook_admin_paths_alter(&$paths) {
  507. // Treat all user pages as administrative.
  508. $paths['user'] = TRUE;
  509. $paths['user/*'] = TRUE;
  510. // Treat the forum topic node form as a non-administrative page.
  511. $paths['node/add/forum'] = FALSE;
  512. }
  513. /**
  514. * Act on entities as they are being prepared for view.
  515. *
  516. * Allows you to operate on multiple entities as they are being prepared for
  517. * view. Only use this if attaching the data during the entity_load() phase
  518. * is not appropriate, for example when attaching other 'entity' style objects.
  519. *
  520. * @param $entities
  521. * The entities keyed by entity ID.
  522. * @param $type
  523. * The type of entities being loaded (i.e. node, user, comment).
  524. * @param $langcode
  525. * The language to display the entity in.
  526. */
  527. function hook_entity_prepare_view($entities, $type, $langcode) {
  528. // Load a specific node into the user object for later theming.
  529. if ($type == 'user') {
  530. $nodes = mymodule_get_user_nodes(array_keys($entities));
  531. foreach ($entities as $uid => $entity) {
  532. $entity->user_node = $nodes[$uid];
  533. }
  534. }
  535. }
  536. /**
  537. * Perform periodic actions.
  538. *
  539. * Modules that require some commands to be executed periodically can
  540. * implement hook_cron(). The engine will then call the hook whenever a cron
  541. * run happens, as defined by the administrator. Typical tasks managed by
  542. * hook_cron() are database maintenance, backups, recalculation of settings
  543. * or parameters, automated mailing, and retrieving remote data.
  544. *
  545. * Short-running or non-resource-intensive tasks can be executed directly in
  546. * the hook_cron() implementation.
  547. *
  548. * Long-running tasks and tasks that could time out, such as retrieving remote
  549. * data, sending email, and intensive file tasks, should use the queue API
  550. * instead of executing the tasks directly. To do this, first define one or
  551. * more queues via hook_cron_queue_info(). Then, add items that need to be
  552. * processed to the defined queues.
  553. */
  554. function hook_cron() {
  555. // Short-running operation example, not using a queue:
  556. // Delete all expired records since the last cron run.
  557. $expires = variable_get('mymodule_cron_last_run', REQUEST_TIME);
  558. db_delete('mymodule_table')
  559. ->condition('expires', $expires, '>=')
  560. ->execute();
  561. variable_set('mymodule_cron_last_run', REQUEST_TIME);
  562. // Long-running operation example, leveraging a queue:
  563. // Fetch feeds from other sites.
  564. $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh <> :never', array(
  565. ':time' => REQUEST_TIME,
  566. ':never' => AGGREGATOR_CLEAR_NEVER,
  567. ));
  568. $queue = DrupalQueue::get('aggregator_feeds');
  569. foreach ($result as $feed) {
  570. $queue->createItem($feed);
  571. }
  572. }
  573. /**
  574. * Declare queues holding items that need to be run periodically.
  575. *
  576. * While there can be only one hook_cron() process running at the same time,
  577. * there can be any number of processes defined here running. Because of
  578. * this, long running tasks are much better suited for this API. Items queued
  579. * in hook_cron() might be processed in the same cron run if there are not many
  580. * items in the queue, otherwise it might take several requests, which can be
  581. * run in parallel.
  582. *
  583. * @return
  584. * An associative array where the key is the queue name and the value is
  585. * again an associative array. Possible keys are:
  586. * - 'worker callback': The name of the function to call. It will be called
  587. * with one argument, the item created via DrupalQueue::createItem() in
  588. * hook_cron().
  589. * - 'time': (optional) How much time Drupal should spend on calling this
  590. * worker in seconds. Defaults to 15.
  591. *
  592. * @see hook_cron()
  593. * @see hook_cron_queue_info_alter()
  594. */
  595. function hook_cron_queue_info() {
  596. $queues['aggregator_feeds'] = array(
  597. 'worker callback' => 'aggregator_refresh',
  598. 'time' => 60,
  599. );
  600. return $queues;
  601. }
  602. /**
  603. * Alter cron queue information before cron runs.
  604. *
  605. * Called by drupal_cron_run() to allow modules to alter cron queue settings
  606. * before any jobs are processesed.
  607. *
  608. * @param array $queues
  609. * An array of cron queue information.
  610. *
  611. * @see hook_cron_queue_info()
  612. * @see drupal_cron_run()
  613. */
  614. function hook_cron_queue_info_alter(&$queues) {
  615. // This site has many feeds so let's spend 90 seconds on each cron run
  616. // updating feeds instead of the default 60.
  617. $queues['aggregator_feeds']['time'] = 90;
  618. }
  619. /**
  620. * Allows modules to declare their own Form API element types and specify their
  621. * default values.
  622. *
  623. * This hook allows modules to declare their own form element types and to
  624. * specify their default values. The values returned by this hook will be
  625. * merged with the elements returned by hook_form() implementations and so
  626. * can return defaults for any Form APIs keys in addition to those explicitly
  627. * mentioned below.
  628. *
  629. * Each of the form element types defined by this hook is assumed to have
  630. * a matching theme function, e.g. theme_elementtype(), which should be
  631. * registered with hook_theme() as normal.
  632. *
  633. * For more information about custom element types see the explanation at
  634. * http://drupal.org/node/169815.
  635. *
  636. * @return
  637. * An associative array describing the element types being defined. The array
  638. * contains a sub-array for each element type, with the machine-readable type
  639. * name as the key. Each sub-array has a number of possible attributes:
  640. * - "#input": boolean indicating whether or not this element carries a value
  641. * (even if it's hidden).
  642. * - "#process": array of callback functions taking $element, $form_state,
  643. * and $complete_form.
  644. * - "#after_build": array of callback functions taking $element and $form_state.
  645. * - "#validate": array of callback functions taking $form and $form_state.
  646. * - "#element_validate": array of callback functions taking $element and
  647. * $form_state.
  648. * - "#pre_render": array of callback functions taking $element and $form_state.
  649. * - "#post_render": array of callback functions taking $element and $form_state.
  650. * - "#submit": array of callback functions taking $form and $form_state.
  651. * - "#title_display": optional string indicating if and how #title should be
  652. * displayed, see theme_form_element() and theme_form_element_label().
  653. *
  654. * @see hook_element_info_alter()
  655. * @see system_element_info()
  656. */
  657. function hook_element_info() {
  658. $types['filter_format'] = array(
  659. '#input' => TRUE,
  660. );
  661. return $types;
  662. }
  663. /**
  664. * Alter the element type information returned from modules.
  665. *
  666. * A module may implement this hook in order to alter the element type defaults
  667. * defined by a module.
  668. *
  669. * @param $type
  670. * All element type defaults as collected by hook_element_info().
  671. *
  672. * @see hook_element_info()
  673. */
  674. function hook_element_info_alter(&$type) {
  675. // Decrease the default size of textfields.
  676. if (isset($type['textfield']['#size'])) {
  677. $type['textfield']['#size'] = 40;
  678. }
  679. }
  680. /**
  681. * Perform cleanup tasks.
  682. *
  683. * This hook is run at the end of each page request. It is often used for
  684. * page logging and specialized cleanup. This hook MUST NOT print anything
  685. * because by the time it runs the response is already sent to the browser.
  686. *
  687. * Only use this hook if your code must run even for cached page views.
  688. * If you have code which must run once on all non-cached pages, use
  689. * hook_init() instead. That is the usual case. If you implement this hook
  690. * and see an error like 'Call to undefined function', it is likely that
  691. * you are depending on the presence of a module which has not been loaded yet.
  692. * It is not loaded because Drupal is still in bootstrap mode.
  693. *
  694. * @param $destination
  695. * If this hook is invoked as part of a drupal_goto() call, then this argument
  696. * will be a fully-qualified URL that is the destination of the redirect.
  697. */
  698. function hook_exit($destination = NULL) {
  699. db_update('counter')
  700. ->expression('hits', 'hits + 1')
  701. ->condition('type', 1)
  702. ->execute();
  703. }
  704. /**
  705. * Perform necessary alterations to the JavaScript before it is presented on
  706. * the page.
  707. *
  708. * @param $javascript
  709. * An array of all JavaScript being presented on the page.
  710. *
  711. * @see drupal_add_js()
  712. * @see drupal_get_js()
  713. * @see drupal_js_defaults()
  714. */
  715. function hook_js_alter(&$javascript) {
  716. // Swap out jQuery to use an updated version of the library.
  717. $javascript['misc/jquery.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js';
  718. }
  719. /**
  720. * Registers JavaScript/CSS libraries associated with a module.
  721. *
  722. * Modules implementing this return an array of arrays. The key to each
  723. * sub-array is the machine readable name of the library. Each library may
  724. * contain the following items:
  725. *
  726. * - 'title': The human readable name of the library.
  727. * - 'website': The URL of the library's web site.
  728. * - 'version': A string specifying the version of the library; intentionally
  729. * not a float because a version like "1.2.3" is not a valid float. Use PHP's
  730. * version_compare() to compare different versions.
  731. * - 'js': An array of JavaScript elements; each element's key is used as $data
  732. * argument, each element's value is used as $options array for
  733. * drupal_add_js(). To add library-specific (not module-specific) JavaScript
  734. * settings, the key may be skipped, the value must specify
  735. * 'type' => 'setting', and the actual settings must be contained in a 'data'
  736. * element of the value.
  737. * - 'css': Like 'js', an array of CSS elements passed to drupal_add_css().
  738. * - 'dependencies': An array of libraries that are required for a library. Each
  739. * element is an array listing the module and name of another library. Note
  740. * that all dependencies for each dependent library will also be added when
  741. * this library is added.
  742. *
  743. * Registered information for a library should contain re-usable data only.
  744. * Module- or implementation-specific data and integration logic should be added
  745. * separately.
  746. *
  747. * @return
  748. * An array defining libraries associated with a module.
  749. *
  750. * @see system_library()
  751. * @see drupal_add_library()
  752. * @see drupal_get_library()
  753. */
  754. function hook_library() {
  755. // Library One.
  756. $libraries['library-1'] = array(
  757. 'title' => 'Library One',
  758. 'website' => 'http://example.com/library-1',
  759. 'version' => '1.2',
  760. 'js' => array(
  761. drupal_get_path('module', 'my_module') . '/library-1.js' => array(),
  762. ),
  763. 'css' => array(
  764. drupal_get_path('module', 'my_module') . '/library-2.css' => array(
  765. 'type' => 'file',
  766. 'media' => 'screen',
  767. ),
  768. ),
  769. );
  770. // Library Two.
  771. $libraries['library-2'] = array(
  772. 'title' => 'Library Two',
  773. 'website' => 'http://example.com/library-2',
  774. 'version' => '3.1-beta1',
  775. 'js' => array(
  776. // JavaScript settings may use the 'data' key.
  777. array(
  778. 'type' => 'setting',
  779. 'data' => array('library2' => TRUE),
  780. ),
  781. ),
  782. 'dependencies' => array(
  783. // Require jQuery UI core by System module.
  784. array('system', 'ui'),
  785. // Require our other library.
  786. array('my_module', 'library-1'),
  787. // Require another library.
  788. array('other_module', 'library-3'),
  789. ),
  790. );
  791. return $libraries;
  792. }
  793. /**
  794. * Alters the JavaScript/CSS library registry.
  795. *
  796. * Allows certain, contributed modules to update libraries to newer versions
  797. * while ensuring backwards compatibility. In general, such manipulations should
  798. * only be done by designated modules, since most modules that integrate with a
  799. * certain library also depend on the API of a certain library version.
  800. *
  801. * @param $libraries
  802. * The JavaScript/CSS libraries provided by $module. Keyed by internal library
  803. * name and passed by reference.
  804. * @param $module
  805. * The name of the module that registered the libraries.
  806. *
  807. * @see hook_library()
  808. */
  809. function hook_library_alter(&$libraries, $module) {
  810. // Update Farbtastic to version 2.0.
  811. if ($module == 'system' && isset($libraries['farbtastic'])) {
  812. // Verify existing version is older than the one we are updating to.
  813. if (version_compare($libraries['farbtastic']['version'], '2.0', '<')) {
  814. // Update the existing Farbtastic to version 2.0.
  815. $libraries['farbtastic']['version'] = '2.0';
  816. $libraries['farbtastic']['js'] = array(
  817. drupal_get_path('module', 'farbtastic_update') . '/farbtastic-2.0.js' => array(),
  818. );
  819. }
  820. }
  821. }
  822. /**
  823. * Alter CSS files before they are output on the page.
  824. *
  825. * @param $css
  826. * An array of all CSS items (files and inline CSS) being requested on the page.
  827. *
  828. * @see drupal_add_css()
  829. * @see drupal_get_css()
  830. */
  831. function hook_css_alter(&$css) {
  832. // Remove defaults.css file.
  833. unset($css[drupal_get_path('module', 'system') . '/defaults.css']);
  834. }
  835. /**
  836. * Alter the commands that are sent to the user through the Ajax framework.
  837. *
  838. * @param $commands
  839. * An array of all commands that will be sent to the user.
  840. *
  841. * @see ajax_render()
  842. */
  843. function hook_ajax_render_alter($commands) {
  844. // Inject any new status messages into the content area.
  845. $commands[] = ajax_command_prepend('#block-system-main .content', theme('status_messages'));
  846. }
  847. /**
  848. * Add elements to a page before it is rendered.
  849. *
  850. * Use this hook when you want to add elements at the page level. For your
  851. * additions to be printed, they have to be placed below a top level array key
  852. * of the $page array that has the name of a region of the active theme.
  853. *
  854. * By default, valid region keys are 'page_top', 'header', 'sidebar_first',
  855. * 'content', 'sidebar_second' and 'page_bottom'. To get a list of all regions
  856. * of the active theme, use system_region_list($theme). Note that $theme is a
  857. * global variable.
  858. *
  859. * If you want to alter the elements added by other modules or if your module
  860. * depends on the elements of other modules, use hook_page_alter() instead which
  861. * runs after this hook.
  862. *
  863. * @param $page
  864. * Nested array of renderable elements that make up the page.
  865. *
  866. * @see hook_page_alter()
  867. * @see drupal_render_page()
  868. */
  869. function hook_page_build(&$page) {
  870. if (menu_get_object('node', 1)) {
  871. // We are on a node detail page. Append a standard disclaimer to the
  872. // content region.
  873. $page['content']['disclaimer'] = array(
  874. '#markup' => t('Acme, Inc. is not responsible for the contents of this sample code.'),
  875. '#weight' => 25,
  876. );
  877. }
  878. }
  879. /**
  880. * Alter a menu router item right after it has been retrieved from the database or cache.
  881. *
  882. * This hook is invoked by menu_get_item() and allows for run-time alteration of router
  883. * information (page_callback, title, and so on) before it is translated and checked for
  884. * access. The passed-in $router_item is statically cached for the current request, so this
  885. * hook is only invoked once for any router item that is retrieved via menu_get_item().
  886. *
  887. * Usually, modules will only want to inspect the router item and conditionally
  888. * perform other actions (such as preparing a state for the current request).
  889. * Note that this hook is invoked for any router item that is retrieved by
  890. * menu_get_item(), which may or may not be called on the path itself, so implementations
  891. * should check the $path parameter if the alteration should fire for the current request
  892. * only.
  893. *
  894. * @param $router_item
  895. * The menu router item for $path.
  896. * @param $path
  897. * The originally passed path, for which $router_item is responsible.
  898. * @param $original_map
  899. * The path argument map, as contained in $path.
  900. *
  901. * @see menu_get_item()
  902. */
  903. function hook_menu_get_item_alter(&$router_item, $path, $original_map) {
  904. // When retrieving the router item for the current path...
  905. if ($path == $_GET['q']) {
  906. // ...call a function that prepares something for this request.
  907. mymodule_prepare_something();
  908. }
  909. }
  910. /**
  911. * Define menu items and page callbacks.
  912. *
  913. * This hook enables modules to register paths in order to define how URL
  914. * requests are handled. Paths may be registered for URL handling only, or they
  915. * can register a link to be placed in a menu (usually the Navigation menu). A
  916. * path and its associated information is commonly called a "menu router item".
  917. * This hook is rarely called (for example, when modules are enabled), and
  918. * its results are cached in the database.
  919. *
  920. * hook_menu() implementations return an associative array whose keys define
  921. * paths and whose values are an associative array of properties for each
  922. * path. (The complete list of properties is in the return value section below.)
  923. *
  924. * The definition for each path may include a page callback function, which is
  925. * invoked when the registered path is requested. If there is no other
  926. * registered path that fits the requested path better, any further path
  927. * components are passed to the callback function. For example, your module
  928. * could register path 'abc/def':
  929. * @code
  930. * function mymodule_menu() {
  931. * $items['abc/def'] = array(
  932. * 'page callback' => 'mymodule_abc_view',
  933. * );
  934. * return $items;
  935. * }
  936. *
  937. * function mymodule_abc_view($ghi = 0, $jkl = '') {
  938. * // ...
  939. * }
  940. * @endcode
  941. * When path 'abc/def' is requested, no further path components are in the
  942. * request, and no additional arguments are passed to the callback function (so
  943. * $ghi and $jkl would take the default values as defined in the function
  944. * signature). When 'abc/def/123/foo' is requested, $ghi will be '123' and
  945. * $jkl will be 'foo'. Note that this automatic passing of optional path
  946. * arguments applies only to page and theme callback functions.
  947. *
  948. * In addition to optional path arguments, the page callback and other callback
  949. * functions may specify argument lists as arrays. These argument lists may
  950. * contain both fixed/hard-coded argument values and integers that correspond
  951. * to path components. When integers are used and the callback function is
  952. * called, the corresponding path components will be substituted for the
  953. * integers. That is, the integer 0 in an argument list will be replaced with
  954. * the first path component, integer 1 with the second, and so on (path
  955. * components are numbered starting from zero). To pass an integer without it
  956. * being replaced with its respective path component, use the string value of
  957. * the integer (e.g., '1') as the argument value. This substitution feature
  958. * allows you to re-use a callback function for several different paths. For
  959. * example:
  960. * @code
  961. * function mymodule_menu() {
  962. * $items['abc/def'] = array(
  963. * 'page callback' => 'mymodule_abc_view',
  964. * 'page arguments' => array(1, 'foo'),
  965. * );
  966. * return $items;
  967. * }
  968. * @endcode
  969. * When path 'abc/def' is requested, the page callback function will get 'def'
  970. * as the first argument and (always) 'foo' as the second argument.
  971. *
  972. * If a page callback function uses an argument list array, and its path is
  973. * requested with optional path arguments, then the list array's arguments are
  974. * passed to the callback function first, followed by the optional path
  975. * arguments. Using the above example, when path 'abc/def/bar/baz' is requested,
  976. * mymodule_abc_view() will be called with 'def', 'foo', 'bar' and 'baz' as
  977. * arguments, in that order.
  978. *
  979. * Special care should be taken for the page callback drupal_get_form(), because
  980. * your specific form callback function will always receive $form and
  981. * &$form_state as the first function arguments:
  982. * @code
  983. * function mymodule_abc_form($form, &$form_state) {
  984. * // ...
  985. * return $form;
  986. * }
  987. * @endcode
  988. * See @link form_api Form API documentation @endlink for details.
  989. *
  990. * Wildcards within paths also work with integer substitution. For example,
  991. * your module could register path 'my-module/%/edit':
  992. * @code
  993. * $items['my-module/%/edit'] = array(
  994. * 'page callback' => 'mymodule_abc_edit',
  995. * 'page arguments' => array(1),
  996. * );
  997. * @endcode
  998. * When path 'my-module/foo/edit' is requested, integer 1 will be replaced
  999. * with 'foo' and passed to the callback function. Note that wildcards may not
  1000. * be used as the first component.
  1001. *
  1002. * Registered paths may also contain special "auto-loader" wildcard components
  1003. * in the form of '%mymodule_abc', where the '%' part means that this path
  1004. * component is a wildcard, and the 'mymodule_abc' part defines the prefix for a
  1005. * load function, which here would be named mymodule_abc_load(). When a matching
  1006. * path is requested, your load function will receive as its first argument the
  1007. * path component in the position of the wildcard; load functions may also be
  1008. * passed additional arguments (see "load arguments" in the return value
  1009. * section below). For example, your module could register path
  1010. * 'my-module/%mymodule_abc/edit':
  1011. * @code
  1012. * $items['my-module/%mymodule_abc/edit'] = array(
  1013. * 'page callback' => 'mymodule_abc_edit',
  1014. * 'page arguments' => array(1),
  1015. * );
  1016. * @endcode
  1017. * When path 'my-module/123/edit' is requested, your load function
  1018. * mymodule_abc_load() will be invoked with the argument '123', and should
  1019. * load and return an "abc" object with internal id 123:
  1020. * @code
  1021. * function mymodule_abc_load($abc_id) {
  1022. * return db_query("SELECT * FROM {mymodule_abc} WHERE abc_id = :abc_id", array(':abc_id' => $abc_id))->fetchObject();
  1023. * }
  1024. * @endcode
  1025. * This 'abc' object will then be passed into the callback functions defined
  1026. * for the menu item, such as the page callback function mymodule_abc_edit()
  1027. * to replace the integer 1 in the argument array. Note that a load function
  1028. * should return FALSE when it is unable to provide a loadable object. For
  1029. * example, the node_load() function for the 'node/%node/edit' menu item will
  1030. * return FALSE for the path 'node/999/edit' if a node with a node ID of 999
  1031. * does not exist. The menu routing system will return a 404 error in this case.
  1032. *
  1033. * You can also define a %wildcard_to_arg() function (for the example menu
  1034. * entry above this would be 'mymodule_abc_to_arg()'). The _to_arg() function
  1035. * is invoked to retrieve a value that is used in the path in place of the
  1036. * wildcard. A good example is user.module, which defines
  1037. * user_uid_optional_to_arg() (corresponding to the menu entry
  1038. * 'tracker/%user_uid_optional'). This function returns the user ID of the
  1039. * current user.
  1040. *
  1041. * The _to_arg() function will get called with three arguments:
  1042. * - $arg: A string representing whatever argument may have been supplied by
  1043. * the caller (this is particularly useful if you want the _to_arg()
  1044. * function only supply a (default) value if no other value is specified,
  1045. * as in the case of user_uid_optional_to_arg().
  1046. * - $map: An array of all path fragments (e.g. array('node','123','edit') for
  1047. * 'node/123/edit').
  1048. * - $index: An integer indicating which element of $map corresponds to $arg.
  1049. *
  1050. * _load() and _to_arg() functions may seem similar at first glance, but they
  1051. * have different purposes and are called at different times. _load()
  1052. * functions are called when the menu system is collecting arguments to pass
  1053. * to the callback functions defined for the menu item. _to_arg() functions
  1054. * are called when the menu system is generating links to related paths, such
  1055. * as the tabs for a set of MENU_LOCAL_TASK items.
  1056. *
  1057. * You can also make groups of menu items to be rendered (by default) as tabs
  1058. * on a page. To do that, first create one menu item of type MENU_NORMAL_ITEM,
  1059. * with your chosen path, such as 'foo'. Then duplicate that menu item, using a
  1060. * subdirectory path, such as 'foo/tab1', and changing the type to
  1061. * MENU_DEFAULT_LOCAL_TASK to make it the default tab for the group. Then add
  1062. * the additional tab items, with paths such as "foo/tab2" etc., with type
  1063. * MENU_LOCAL_TASK. Example:
  1064. * @code
  1065. * // Make "Foo settings" appear on the admin Config page
  1066. * $items['admin/config/system/foo'] = array(
  1067. * 'title' => 'Foo settings',
  1068. * 'type' => MENU_NORMAL_ITEM,
  1069. * // Page callback, etc. need to be added here.
  1070. * );
  1071. * // Make "Tab 1" the main tab on the "Foo settings" page
  1072. * $items['admin/config/system/foo/tab1'] = array(
  1073. * 'title' => 'Tab 1',
  1074. * 'type' => MENU_DEFAULT_LOCAL_TASK,
  1075. * // Access callback, page callback, and theme callback will be inherited
  1076. * // from 'admin/config/system/foo', if not specified here to override.
  1077. * );
  1078. * // Make an additional tab called "Tab 2" on "Foo settings"
  1079. * $items['admin/config/system/foo/tab2'] = array(
  1080. * 'title' => 'Tab 2',
  1081. * 'type' => MENU_LOCAL_TASK,
  1082. * // Page callback and theme callback will be inherited from
  1083. * // 'admin/config/system/foo', if not specified here to override.
  1084. * // Need to add access callback or access arguments.
  1085. * );
  1086. * @endcode
  1087. *
  1088. * @return
  1089. * An array of menu items. Each menu item has a key corresponding to the
  1090. * Drupal path being registered. The corresponding array value is an
  1091. * associative array that may contain the following key-value pairs:
  1092. * - "title": Required. The untranslated title of the menu item.
  1093. * - "title callback": Function to generate the title; defaults to t().
  1094. * If you require only the raw string to be output, set this to FALSE.
  1095. * - "title arguments": Arguments to send to t() or your custom callback,
  1096. * with path component substitution as described above.
  1097. * - "description": The untranslated description of the menu item.
  1098. * - "page callback": The function to call to display a web page when the user
  1099. * visits the path. If omitted, the parent menu item's callback will be used
  1100. * instead.
  1101. * - "page arguments": An array of arguments to pass to the page callback
  1102. * function, with path component substitution as described above.
  1103. * - "delivery callback": The function to call to package the result of the
  1104. * page callback function and send it to the browser. Defaults to
  1105. * drupal_deliver_html_page() unless a value is inherited from a parent menu
  1106. * item. Note that this function is called even if the access checks fail,
  1107. * so any custom delivery callback function should take that into account.
  1108. * See drupal_deliver_html_page() for an example.
  1109. * - "access callback": A function returning TRUE if the user has access
  1110. * rights to this menu item, and FALSE if not. It can also be a boolean
  1111. * constant instead of a function, and you can also use numeric values
  1112. * (will be cast to boolean). Defaults to user_access() unless a value is
  1113. * inherited from the parent menu item; only MENU_DEFAULT_LOCAL_TASK items
  1114. * can inherit access callbacks. To use the user_access() default callback,
  1115. * you must specify the permission to check as 'access arguments' (see
  1116. * below).
  1117. * - "access arguments": An array of arguments to pass to the access callback
  1118. * function, with path component substitution as described above. If the
  1119. * access callback is inherited (see above), the access arguments will be
  1120. * inherited with it, unless overridden in the child menu item.
  1121. * - "theme callback": (optional) A function returning the machine-readable
  1122. * name of the theme that will be used to render the page. If not provided,
  1123. * the value will be inherited from a parent menu item. If there is no
  1124. * theme callback, or if the function does not return the name of a current
  1125. * active theme on the site, the theme for this page will be determined by
  1126. * either hook_custom_theme() or the default theme instead. As a general
  1127. * rule, the use of theme callback functions should be limited to pages
  1128. * whose functionality is very closely tied to a particular theme, since
  1129. * they can only be overridden by modules which specifically target those
  1130. * pages in hook_menu_alter(). Modules implementing more generic theme
  1131. * switching functionality (for example, a module which allows the theme to
  1132. * be set dynamically based on the current user's role) should use
  1133. * hook_custom_theme() instead.
  1134. * - "theme arguments": An array of arguments to pass to the theme callback
  1135. * function, with path component substitution as described above.
  1136. * - "file": A file that will be included before the page callback is called;
  1137. * this allows page callback functions to be in separate files. The file
  1138. * should be relative to the implementing module's directory unless
  1139. * otherwise specified by the "file path" option. Does not apply to other
  1140. * callbacks (only page callback).
  1141. * - "file path": The path to the directory containing the file specified in
  1142. * "file". This defaults to the path to the module implementing the hook.
  1143. * - "load arguments": An array of arguments to be passed to each of the
  1144. * wildcard object loaders in the path, after the path argument itself.
  1145. * For example, if a module registers path node/%node/revisions/%/view
  1146. * with load arguments set to array(3), the '%node' in the path indicates
  1147. * that the loader function node_load() will be called with the second
  1148. * path component as the first argument. The 3 in the load arguments
  1149. * indicates that the fourth path component will also be passed to
  1150. * node_load() (numbering of path components starts at zero). So, if path
  1151. * node/12/revisions/29/view is requested, node_load(12, 29) will be called.
  1152. * There are also two "magic" values that can be used in load arguments.
  1153. * "%index" indicates the index of the wildcard path component. "%map"
  1154. * indicates the path components as an array. For example, if a module
  1155. * registers for several paths of the form 'user/%user_category/edit/*', all
  1156. * of them can use the same load function user_category_load(), by setting
  1157. * the load arguments to array('%map', '%index'). For instance, if the user
  1158. * is editing category 'foo' by requesting path 'user/32/edit/foo', the load
  1159. * function user_category_load() will be called with 32 as its first
  1160. * argument, the array ('user', 32, 'edit', 'foo') as the map argument,
  1161. * and 1 as the index argument (because %user_category is the second path
  1162. * component and numbering starts at zero). user_category_load() can then
  1163. * use these values to extract the information that 'foo' is the category
  1164. * being requested.
  1165. * - "weight": An integer that determines the relative position of items in
  1166. * the menu; higher-weighted items sink. Defaults to 0. Menu items with the
  1167. * same weight are ordered alphabetically.
  1168. * - "menu_name": Optional. Set this to a custom menu if you don't want your
  1169. * item to be placed in Navigation.
  1170. * - "expanded": Optional. If set to TRUE, and if a menu link is provided for
  1171. * this menu item (as a result of other properties), then the menu link is
  1172. * always expanded, equivalent to its 'always expanded' checkbox being set
  1173. * in the UI.
  1174. * - "context": (optional) Defines the context a tab may appear in. By
  1175. * default, all tabs are only displayed as local tasks when being rendered
  1176. * in a page context. All tabs that should be accessible as contextual links
  1177. * in page region containers outside of the parent menu item's primary page
  1178. * context should be registered using one of the following contexts:
  1179. * - MENU_CONTEXT_PAGE: (default) The tab is displayed as local task for the
  1180. * page context only.
  1181. * - MENU_CONTEXT_INLINE: The tab is displayed as contextual link outside of
  1182. * the primary page context only.
  1183. * Contexts can be combined. For example, to display a tab both on a page
  1184. * and inline, a menu router item may specify:
  1185. * @code
  1186. * 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  1187. * @endcode
  1188. * - "tab_parent": For local task menu items, the path of the task's parent
  1189. * item; defaults to the same path without the last component (e.g., the
  1190. * default parent for 'admin/people/create' is 'admin/people').
  1191. * - "tab_root": For local task menu items, the path of the closest non-tab
  1192. * item; same default as "tab_parent".
  1193. * - "position": Position of the block ('left' or 'right') on the system
  1194. * administration page for this item.
  1195. * - "type": A bitmask of flags describing properties of the menu item.
  1196. * Many shortcut bitmasks are provided as constants in menu.inc:
  1197. * - MENU_NORMAL_ITEM: Normal menu items show up in the menu tree and can be
  1198. * moved/hidden by the administrator.
  1199. * - MENU_CALLBACK: Callbacks simply register a path so that the correct
  1200. * information is generated when the path is accessed.
  1201. * - MENU_SUGGESTED_ITEM: Modules may "suggest" menu items that the
  1202. * administrator may enable.
  1203. * - MENU_LOCAL_ACTION: Local actions are menu items that describe actions
  1204. * on the parent item such as adding a new user or block, and are
  1205. * rendered in the action-links list in your theme.
  1206. * - MENU_LOCAL_TASK: Local tasks are menu items that describe different
  1207. * displays of data, and are generally rendered as tabs.
  1208. * - MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one
  1209. * "default" task, which should display the same page as the parent item.
  1210. * If the "type" element is omitted, MENU_NORMAL_ITEM is assumed.
  1211. * - "options": An array of options to be passed to l() when generating a link
  1212. * from this menu item. Note that the "options" parameter has no effect on
  1213. * MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, and MENU_LOCAL_ACTION items.
  1214. *
  1215. * For a detailed usage example, see page_example.module.
  1216. * For comprehensive documentation on the menu system, see
  1217. * http://drupal.org/node/102338.
  1218. */
  1219. function hook_menu() {
  1220. $items['example'] = array(
  1221. 'title' => 'Example Page',
  1222. 'page callback' => 'example_page',
  1223. 'access arguments' => array('access content'),
  1224. 'type' => MENU_SUGGESTED_ITEM,
  1225. );
  1226. $items['example/feed'] = array(
  1227. 'title' => 'Example RSS feed',
  1228. 'page callback' => 'example_feed',
  1229. 'access arguments' => array('access content'),
  1230. 'type' => MENU_CALLBACK,
  1231. );
  1232. return $items;
  1233. }
  1234. /**
  1235. * Alter the data being saved to the {menu_router} table after hook_menu is invoked.
  1236. *
  1237. * This hook is invoked by menu_router_build(). The menu definitions are passed
  1238. * in by reference. Each element of the $items array is one item returned
  1239. * by a module from hook_menu. Additional items may be added, or existing items
  1240. * altered.
  1241. *
  1242. * @param $items
  1243. * Associative array of menu router definitions returned from hook_menu().
  1244. */
  1245. function hook_menu_alter(&$items) {
  1246. // Example - disable the page at node/add
  1247. $items['node/add']['access callback'] = FALSE;
  1248. }
  1249. /**
  1250. * Alter the data being saved to the {menu_links} table by menu_link_save().
  1251. *
  1252. * @param $item
  1253. * Associative array defining a menu link as passed into menu_link_save().
  1254. *
  1255. * @see hook_translated_menu_link_alter()
  1256. */
  1257. function hook_menu_link_alter(&$item) {
  1258. // Make all new admin links hidden (a.k.a disabled).
  1259. if (strpos($item['link_path'], 'admin') === 0 && empty($item['mlid'])) {
  1260. $item['hidden'] = 1;
  1261. }
  1262. // Flag a link to be altered by hook_translated_menu_link_alter().
  1263. if ($item['link_path'] == 'devel/cache/clear') {
  1264. $item['options']['alter'] = TRUE;
  1265. }
  1266. // Flag a link to be altered by hook_translated_menu_link_alter(), but only
  1267. // if it is derived from a menu router item; i.e., do not alter a custom
  1268. // menu link pointing to the same path that has been created by a user.
  1269. if ($item['link_path'] == 'user' && $item['module'] == 'system') {
  1270. $item['options']['alter'] = TRUE;
  1271. }
  1272. }
  1273. /**
  1274. * Alter a menu link after it has been translated and before it is rendered.
  1275. *
  1276. * This hook is invoked from _menu_link_translate() after a menu link has been
  1277. * translated; i.e., after dynamic path argument placeholders (%) have been
  1278. * replaced with actual values, the user access to the link's target page has
  1279. * been checked, and the link has been localized. It is only invoked if
  1280. * $item['options']['alter'] has been set to a non-empty value (e.g., TRUE).
  1281. * This flag should be set using hook_menu_link_alter().
  1282. *
  1283. * Implementations of this hook are able to alter any property of the menu link.
  1284. * For example, this hook may be used to add a page-specific query string to all
  1285. * menu links, or hide a certain link by setting:
  1286. * @code
  1287. * 'hidden' => 1,
  1288. * @endcode
  1289. *
  1290. * @param $item
  1291. * Associative array defining a menu link after _menu_link_translate()
  1292. * @param $map
  1293. * Associative array containing the menu $map (path parts and/or objects).
  1294. *
  1295. * @see hook_menu_link_alter()
  1296. */
  1297. function hook_translated_menu_link_alter(&$item, $map) {
  1298. if ($item['href'] == 'devel/cache/clear') {
  1299. $item['localized_options']['query'] = drupal_get_destination();
  1300. }
  1301. }
  1302. /**
  1303. * Inform modules that a menu link has been created.
  1304. *
  1305. * This hook is used to notify modules that menu items have been
  1306. * created. Contributed modules may use the information to perform
  1307. * actions based on the information entered into the menu system.
  1308. *
  1309. * @param $link
  1310. * Associative array defining a menu link as passed into menu_link_save().
  1311. *
  1312. * @see hook_menu_link_update()
  1313. * @see hook_menu_link_delete()
  1314. */
  1315. function hook_menu_link_insert($link) {
  1316. // In our sample case, we track menu items as editing sections
  1317. // of the site. These are stored in our table as 'disabled' items.
  1318. $record['mlid'] = $link['mlid'];
  1319. $record['menu_name'] = $link['menu_name'];
  1320. $record['status'] = 0;
  1321. drupal_write_record('menu_example', $record);
  1322. }
  1323. /**
  1324. * Inform modules that a menu link has been updated.
  1325. *
  1326. * This hook is used to notify modules that menu items have been
  1327. * updated. Contributed modules may use the information to perform
  1328. * actions based on the information entered into the menu system.
  1329. *
  1330. * @param $link
  1331. * Associative array defining a menu link as passed into menu_link_save().
  1332. *
  1333. * @see hook_menu_link_insert()
  1334. * @see hook_menu_link_delete()
  1335. */
  1336. function hook_menu_link_update($link) {
  1337. // If the parent menu has changed, update our record.
  1338. $menu_name = db_query("SELECT menu_name FROM {menu_example} WHERE mlid = :mlid", array(':mlid' => $link['mlid']))->fetchField();
  1339. if ($menu_name != $link['menu_name']) {
  1340. db_update('menu_example')
  1341. ->fields(array('menu_name' => $link['menu_name']))
  1342. ->condition('mlid', $link['mlid'])
  1343. ->execute();
  1344. }
  1345. }
  1346. /**
  1347. * Inform modules that a menu link has been deleted.
  1348. *
  1349. * This hook is used to notify modules that menu items have been
  1350. * deleted. Contributed modules may use the information to perform
  1351. * actions based on the information entered into the menu system.
  1352. *
  1353. * @param $link
  1354. * Associative array defining a menu link as passed into menu_link_save().
  1355. *
  1356. * @see hook_menu_link_insert()
  1357. * @see hook_menu_link_update()
  1358. */
  1359. function hook_menu_link_delete($link) {
  1360. // Delete the record from our table.
  1361. db_delete('menu_example')
  1362. ->condition('mlid', $link['mlid'])
  1363. ->execute();
  1364. }
  1365. /**
  1366. * Alter tabs and actions displayed on the page before they are rendered.
  1367. *
  1368. * This hook is invoked by menu_local_tasks(). The system-determined tabs and
  1369. * actions are passed in by reference. Additional tabs or actions may be added,
  1370. * or existing items altered.
  1371. *
  1372. * Each tab or action is an associative array containing:
  1373. * - #theme: The theme function to use to render.
  1374. * - #link: An associative array containing:
  1375. * - title: The localized title of the link.
  1376. * - href: The system path to link to.
  1377. * - localized_options: An array of options to pass to l().
  1378. * - #active: Whether the link should be marked as 'active'.
  1379. *
  1380. * @param $data
  1381. * An associative array containing:
  1382. * - actions: An associative array containing:
  1383. * - count: The amount of actions determined by the menu system, which can
  1384. * be ignored.
  1385. * - output: A list of of actions, each one being an associative array
  1386. * as described above.
  1387. * - tabs: An indexed array (list) of tab levels (up to 2 levels), each
  1388. * containing an associative array:
  1389. * - count: The amount of tabs determined by the menu system. This value
  1390. * does not need to be altered if there is more than one tab.
  1391. * - output: A list of of tabs, each one being an associative array as
  1392. * described above.
  1393. * @param $router_item
  1394. * The menu system router item of the page.
  1395. * @param $root_path
  1396. * The path to the root item for this set of tabs.
  1397. */
  1398. function hook_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  1399. // Add an action linking to node/add to all pages.
  1400. $data['actions']['output'][] = array(
  1401. '#theme' => 'menu_local_task',
  1402. '#link' => array(
  1403. 'title' => t('Add new content'),
  1404. 'href' => 'node/add',
  1405. 'localized_options' => array(
  1406. 'attributes' => array(
  1407. 'title' => t('Add new content'),
  1408. ),
  1409. ),
  1410. ),
  1411. );
  1412. // Add a tab linking to node/add to all pages.
  1413. $data['tabs'][0]['output'][] = array(
  1414. '#theme' => 'menu_local_task',
  1415. '#link' => array(
  1416. 'title' => t('Example tab'),
  1417. 'href' => 'node/add',
  1418. 'localized_options' => array(
  1419. 'attributes' => array(
  1420. 'title' => t('Add new content'),
  1421. ),
  1422. ),
  1423. ),
  1424. // Define whether this link is active. This can be omitted for
  1425. // implementations that add links to pages outside of the current page
  1426. // context.
  1427. '#active' => ($router_item['path'] == $root_path),
  1428. );
  1429. }
  1430. /**
  1431. * Alter links in the active trail before it is rendered as the breadcrumb.
  1432. *
  1433. * This hook is invoked by menu_get_active_breadcrumb() and allows alteration
  1434. * of the breadcrumb links for the current page, which may be preferred instead
  1435. * of setting a custom breadcrumb via drupal_set_breadcrumb().
  1436. *
  1437. * Implementations should take into account that menu_get_active_breadcrumb()
  1438. * subsequently performs the following adjustments to the active trail *after*
  1439. * this hook has been invoked:
  1440. * - The last link in $active_trail is removed, if its 'href' is identical to
  1441. * the 'href' of $item. This happens, because the breadcrumb normally does
  1442. * not contain a link to the current page.
  1443. * - The (second to) last link in $active_trail is removed, if the current $item
  1444. * is a MENU_DEFAULT_LOCAL_TASK. This happens in order to do not show a link
  1445. * to the current page, when being on the path for the default local task;
  1446. * e.g. when being on the path node/%/view, the breadcrumb should not contain
  1447. * a link to node/%.
  1448. *
  1449. * Each link in the active trail must contain:
  1450. * - title: The localized title of the link.
  1451. * - href: The system path to link to.
  1452. * - localized_options: An array of options to pass to url().
  1453. *
  1454. * @param $active_trail
  1455. * An array containing breadcrumb links for the current page.
  1456. * @param $item
  1457. * The menu router item of the current page.
  1458. *
  1459. * @see drupal_set_breadcrumb()
  1460. * @see menu_get_active_breadcrumb()
  1461. * @see menu_get_active_trail()
  1462. * @see menu_set_active_trail()
  1463. */
  1464. function hook_menu_breadcrumb_alter(&$active_trail, $item) {
  1465. // Always display a link to the current page by duplicating the last link in
  1466. // the active trail. This means that menu_get_active_breadcrumb() will remove
  1467. // the last link (for the current page), but since it is added once more here,
  1468. // it will appear.
  1469. if (!drupal_is_front_page()) {
  1470. $end = end($active_trail);
  1471. if ($item['href'] == $end['href']) {
  1472. $active_trail[] = $end;
  1473. }
  1474. }
  1475. }
  1476. /**
  1477. * Alter contextual links before they are rendered.
  1478. *
  1479. * This hook is invoked by menu_contextual_links(). The system-determined
  1480. * contextual links are passed in by reference. Additional links may be added
  1481. * or existing links can be altered.
  1482. *
  1483. * Each contextual link must at least contain:
  1484. * - title: The localized title of the link.
  1485. * - href: The system path to link to.
  1486. * - localized_options: An array of options to pass to url().
  1487. *
  1488. * @param $links
  1489. * An associative array containing contextual links for the given $root_path,
  1490. * as described above. The array keys are used to build CSS class names for
  1491. * contextual links and must therefore be unique for each set of contextual
  1492. * links.
  1493. * @param $router_item
  1494. * The menu router item belonging to the $root_path being requested.
  1495. * @param $root_path
  1496. * The (parent) path that has been requested to build contextual links for.
  1497. * This is a normalized path, which means that an originally passed path of
  1498. * 'node/123' became 'node/%'.
  1499. *
  1500. * @see hook_contextual_links_view_alter()
  1501. * @see menu_contextual_links()
  1502. * @see hook_menu()
  1503. * @see contextual_preprocess()
  1504. */
  1505. function hook_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  1506. // Add a link to all contextual links for nodes.
  1507. if ($root_path == 'node/%') {
  1508. $links['foo'] = array(
  1509. 'title' => t('Do fu'),
  1510. 'href' => 'foo/do',
  1511. 'localized_options' => array(
  1512. 'query' => array(
  1513. 'foo' => 'bar',
  1514. ),
  1515. ),
  1516. );
  1517. }
  1518. }
  1519. /**
  1520. * Perform alterations before a page is rendered.
  1521. *
  1522. * Use this hook when you want to remove or alter elements at the page
  1523. * level, or add elements at the page level that depend on an other module's
  1524. * elements (this hook runs after hook_page_build().
  1525. *
  1526. * If you are making changes to entities such as forms, menus, or user
  1527. * profiles, use those objects' native alter hooks instead (hook_form_alter(),
  1528. * for example).
  1529. *
  1530. * The $page array contains top level elements for each block region:
  1531. * @code
  1532. * $page['page_top']
  1533. * $page['header']
  1534. * $page['sidebar_first']
  1535. * $page['content']
  1536. * $page['sidebar_second']
  1537. * $page['page_bottom']
  1538. * @endcode
  1539. *
  1540. * The 'content' element contains the main content of the current page, and its
  1541. * structure will vary depending on what module is responsible for building the
  1542. * page. Some legacy modules may not return structured content at all: their
  1543. * pre-rendered markup will be located in $page['content']['main']['#markup'].
  1544. *
  1545. * Pages built by Drupal's core Node and Blog modules use a standard structure:
  1546. *
  1547. * @code
  1548. * // Node body.
  1549. * $page['content']['system_main']['nodes'][$nid]['body']
  1550. * // Array of links attached to the node (add comments, read more).
  1551. * $page['content']['system_main']['nodes'][$nid]['links']
  1552. * // The node object itself.
  1553. * $page['content']['system_main']['nodes'][$nid]['#node']
  1554. * // The results pager.
  1555. * $page['content']['system_main']['pager']
  1556. * @endcode
  1557. *
  1558. * Blocks may be referenced by their module/delta pair within a region:
  1559. * @code
  1560. * // The login block in the first sidebar region.
  1561. * $page['sidebar_first']['user_login']['#block'];
  1562. * @endcode
  1563. *
  1564. * @param $page
  1565. * Nested array of renderable elements that make up the page.
  1566. *
  1567. * @see hook_page_build()
  1568. * @see drupal_render_page()
  1569. */
  1570. function hook_page_alter(&$page) {
  1571. // Add help text to the user login block.
  1572. $page['sidebar_first']['user_login']['help'] = array(
  1573. '#weight' => -10,
  1574. '#markup' => t('To post comments or add new content, you first have to log in.'),
  1575. );
  1576. }
  1577. /**
  1578. * Perform alterations before a form is rendered.
  1579. *
  1580. * One popular use of this hook is to add form elements to the node form. When
  1581. * altering a node form, the node object can be accessed at $form['#node'].
  1582. *
  1583. * In addition to hook_form_alter(), which is called for all forms, there are
  1584. * two more specific form hooks available. The first,
  1585. * hook_form_BASE_FORM_ID_alter(), allows targeting of a form/forms via a base
  1586. * form (if one exists). The second, hook_form_FORM_ID_alter(), can be used to
  1587. * target a specific form directly.
  1588. *
  1589. * The call order is as follows: all existing form alter functions are called
  1590. * for module A, then all for module B, etc., followed by all for any base
  1591. * theme(s), and finally for the theme itself. The module order is determined
  1592. * by system weight, then by module name.
  1593. *
  1594. * Within each module, form alter hooks are called in the following order:
  1595. * first, hook_form_alter(); second, hook_form_BASE_FORM_ID_alter(); third,
  1596. * hook_form_FORM_ID_alter(). So, for each module, the more general hooks are
  1597. * called first followed by the more specific.
  1598. *
  1599. * @param $form
  1600. * Nested array of form elements that comprise the form.
  1601. * @param $form_state
  1602. * A keyed array containing the current state of the form. The arguments
  1603. * that drupal_get_form() was originally called with are available in the
  1604. * array $form_state['build_info']['args'].
  1605. * @param $form_id
  1606. * String representing the name of the form itself. Typically this is the
  1607. * name of the function that generated the form.
  1608. *
  1609. * @see hook_form_BASE_FORM_ID_alter()
  1610. * @see hook_form_FORM_ID_alter()
  1611. * @see forms_api_reference.html
  1612. */
  1613. function hook_form_alter(&$form, &$form_state, $form_id) {
  1614. if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
  1615. $form['workflow']['upload_' . $form['type']['#value']] = array(
  1616. '#type' => 'radios',
  1617. '#title' => t('Attachments'),
  1618. '#default_value' => variable_get('upload_' . $form['type']['#value'], 1),
  1619. '#options' => array(t('Disabled'), t('Enabled')),
  1620. );
  1621. }
  1622. }
  1623. /**
  1624. * Provide a form-specific alteration instead of the global hook_form_alter().
  1625. *
  1626. * Modules can implement hook_form_FORM_ID_alter() to modify a specific form,
  1627. * rather than implementing hook_form_alter() and checking the form ID, or
  1628. * using long switch statements to alter multiple forms.
  1629. *
  1630. * Form alter hooks are called in the following order: hook_form_alter(),
  1631. * hook_form_BASE_FORM_ID_alter(), hook_form_FORM_ID_alter(). See
  1632. * hook_form_alter() for more details.
  1633. *
  1634. * @param $form
  1635. * Nested array of form elements that comprise the form.
  1636. * @param $form_state
  1637. * A keyed array containing the current state of the form. The arguments
  1638. * that drupal_get_form() was originally called with are available in the
  1639. * array $form_state['build_info']['args'].
  1640. * @param $form_id
  1641. * String representing the name of the form itself. Typically this is the
  1642. * name of the function that generated the form.
  1643. *
  1644. * @see hook_form_alter()
  1645. * @see hook_form_BASE_FORM_ID_alter()
  1646. * @see drupal_prepare_form()
  1647. * @see forms_api_reference.html
  1648. */
  1649. function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  1650. // Modification for the form with the given form ID goes here. For example, if
  1651. // FORM_ID is "user_register_form" this code would run only on the user
  1652. // registration form.
  1653. // Add a checkbox to registration form about agreeing to terms of use.
  1654. $form['terms_of_use'] = array(
  1655. '#type' => 'checkbox',
  1656. '#title' => t("I agree with the website's terms and conditions."),
  1657. '#required' => TRUE,
  1658. );
  1659. }
  1660. /**
  1661. * Provide a form-specific alteration for shared ('base') forms.
  1662. *
  1663. * By default, when drupal_get_form() is called, Drupal looks for a function
  1664. * with the same name as the form ID, and uses that function to build the form.
  1665. * In contrast, base forms allow multiple form IDs to be mapped to a single base
  1666. * (also called 'factory') form function.
  1667. *
  1668. * Modules can implement hook_form_BASE_FORM_ID_alter() to modify a specific
  1669. * base form, rather than implementing hook_form_alter() and checking for
  1670. * conditions that would identify the shared form constructor.
  1671. *
  1672. * To identify the base form ID for a particular form (or to determine whether
  1673. * one exists) check the $form_state. The base form ID is stored under
  1674. * $form_state['build_info']['base_form_id'].
  1675. *
  1676. * See hook_forms() for more information on how to implement base forms in
  1677. * Drupal.
  1678. *
  1679. * Form alter hooks are called in the following order: hook_form_alter(),
  1680. * hook_form_BASE_FORM_ID_alter(), hook_form_FORM_ID_alter(). See
  1681. * hook_form_alter() for more details.
  1682. *
  1683. * @param $form
  1684. * Nested array of form elements that comprise the form.
  1685. * @param $form_state
  1686. * A keyed array containing the current state of the form.
  1687. * @param $form_id
  1688. * String representing the name of the form itself. Typically this is the
  1689. * name of the function that generated the form.
  1690. *
  1691. * @see hook_form_alter()
  1692. * @see hook_form_FORM_ID_alter()
  1693. * @see drupal_prepare_form()
  1694. * @see hook_forms()
  1695. */
  1696. function hook_form_BASE_FORM_ID_alter(&$form, &$form_state, $form_id) {
  1697. // Modification for the form with the given BASE_FORM_ID goes here. For
  1698. // example, if BASE_FORM_ID is "node_form", this code would run on every
  1699. // node form, regardless of node type.
  1700. // Add a checkbox to the node form about agreeing to terms of use.
  1701. $form['terms_of_use'] = array(
  1702. '#type' => 'checkbox',
  1703. '#title' => t("I agree with the website's terms and conditions."),
  1704. '#required' => TRUE,
  1705. );
  1706. }
  1707. /**
  1708. * Map form_ids to form builder functions.
  1709. *
  1710. * By default, when drupal_get_form() is called, the system will look for a
  1711. * function with the same name as the form ID, and use that function to build
  1712. * the form. If no such function is found, Drupal calls this hook. Modules
  1713. * implementing this hook can then provide their own instructions for mapping
  1714. * form IDs to constructor functions. As a result, you can easily map multiple
  1715. * form IDs to a single form constructor (referred to as a 'base' form).
  1716. *
  1717. * Using a base form can help to avoid code duplication, by allowing many
  1718. * similar forms to use the same code base. Another benefit is that it becomes
  1719. * much easier for other modules to apply a general change to the group of
  1720. * forms; hook_form_BASE_FORM_ID_alter() can be used to easily alter multiple
  1721. * forms at once by directly targeting the shared base form.
  1722. *
  1723. * Two example use cases where base forms may be useful are given below.
  1724. *
  1725. * First, you can use this hook to tell the form system to use a different
  1726. * function to build certain forms in your module; this is often used to define
  1727. * a form "factory" function that is used to build several similar forms. In
  1728. * this case, your hook implementation will likely ignore all of the input
  1729. * arguments. See node_forms() for an example of this. Note, node_forms() is the
  1730. * hook_forms() implementation; the base form itself is defined in node_form().
  1731. *
  1732. * Second, you could use this hook to define how to build a form with a
  1733. * dynamically-generated form ID. In this case, you would need to verify that
  1734. * the $form_id input matched your module's format for dynamically-generated
  1735. * form IDs, and if so, act appropriately.
  1736. *
  1737. * @param $form_id
  1738. * The unique string identifying the desired form.
  1739. * @param $args
  1740. * An array containing the original arguments provided to drupal_get_form()
  1741. * or drupal_form_submit(). These are always passed to the form builder and
  1742. * do not have to be specified manually in 'callback arguments'.
  1743. *
  1744. * @return
  1745. * An associative array whose keys define form_ids and whose values are an
  1746. * associative array defining the following keys:
  1747. * - callback: The name of the form builder function to invoke. This will be
  1748. * used for the base form ID, for example, to target a base form using
  1749. * hook_form_BASE_FORM_ID_alter().
  1750. * - callback arguments: (optional) Additional arguments to pass to the
  1751. * function defined in 'callback', which are prepended to $args.
  1752. * - wrapper_callback: (optional) The name of a form builder function to
  1753. * invoke before the form builder defined in 'callback' is invoked. This
  1754. * wrapper callback may prepopulate the $form array with form elements,
  1755. * which will then be already contained in the $form that is passed on to
  1756. * the form builder defined in 'callback'. For example, a wrapper callback
  1757. * could setup wizard-alike form buttons that are the same for a variety of
  1758. * forms that belong to the wizard, which all share the same wrapper
  1759. * callback.
  1760. */
  1761. function hook_forms($form_id, $args) {
  1762. // Simply reroute the (non-existing) $form_id 'mymodule_first_form' to
  1763. // 'mymodule_main_form'.
  1764. $forms['mymodule_first_form'] = array(
  1765. 'callback' => 'mymodule_main_form',
  1766. );
  1767. // Reroute the $form_id and prepend an additional argument that gets passed to
  1768. // the 'mymodule_main_form' form builder function.
  1769. $forms['mymodule_second_form'] = array(
  1770. 'callback' => 'mymodule_main_form',
  1771. 'callback arguments' => array('some parameter'),
  1772. );
  1773. // Reroute the $form_id, but invoke the form builder function
  1774. // 'mymodule_main_form_wrapper' first, so we can prepopulate the $form array
  1775. // that is passed to the actual form builder 'mymodule_main_form'.
  1776. $forms['mymodule_wrapped_form'] = array(
  1777. 'callback' => 'mymodule_main_form',
  1778. 'wrapper_callback' => 'mymodule_main_form_wrapper',
  1779. );
  1780. return $forms;
  1781. }
  1782. /**
  1783. * Perform setup tasks for all page requests.
  1784. *
  1785. * This hook is run at the beginning of the page request. It is typically
  1786. * used to set up global parameters that are needed later in the request.
  1787. *
  1788. * Only use this hook if your code must run even for cached page views. This
  1789. * hook is called before the theme, modules, or most include files are loaded
  1790. * into memory. It happens while Drupal is still in bootstrap mode.
  1791. *
  1792. * @see hook_init()
  1793. */
  1794. function hook_boot() {
  1795. // We need user_access() in the shutdown function. Make sure it gets loaded.
  1796. drupal_load('module', 'user');
  1797. drupal_register_shutdown_function('devel_shutdown');
  1798. }
  1799. /**
  1800. * Perform setup tasks for non-cached page requests.
  1801. *
  1802. * This hook is run at the beginning of the page request. It is typically
  1803. * used to set up global parameters that are needed later in the request.
  1804. * When this hook is called, the theme and all modules are already loaded in
  1805. * memory.
  1806. *
  1807. * This hook is not run on cached pages.
  1808. *
  1809. * To add CSS or JS that should be present on all pages, modules should not
  1810. * implement this hook, but declare these files in their .info file.
  1811. *
  1812. * @see hook_boot()
  1813. */
  1814. function hook_init() {
  1815. // Since this file should only be loaded on the front page, it cannot be
  1816. // declared in the info file.
  1817. if (drupal_is_front_page()) {
  1818. drupal_add_css(drupal_get_path('module', 'foo') . '/foo.css');
  1819. }
  1820. }
  1821. /**
  1822. * Define image toolkits provided by this module.
  1823. *
  1824. * The file which includes each toolkit's functions must be declared as part of
  1825. * the files array in the module .info file so that the registry will find and
  1826. * parse it.
  1827. *
  1828. * The toolkit's functions must be named image_toolkitname_operation().
  1829. * where the operation may be:
  1830. * - 'load': Required. See image_gd_load() for usage.
  1831. * - 'save': Required. See image_gd_save() for usage.
  1832. * - 'settings': Optional. See image_gd_settings() for usage.
  1833. * - 'resize': Optional. See image_gd_resize() for usage.
  1834. * - 'rotate': Optional. See image_gd_rotate() for usage.
  1835. * - 'crop': Optional. See image_gd_crop() for usage.
  1836. * - 'desaturate': Optional. See image_gd_desaturate() for usage.
  1837. *
  1838. * @return
  1839. * An array with the toolkit name as keys and sub-arrays with these keys:
  1840. * - 'title': A string with the toolkit's title.
  1841. * - 'available': A Boolean value to indicate that the toolkit is operating
  1842. * properly, e.g. all required libraries exist.
  1843. *
  1844. * @see system_image_toolkits()
  1845. */
  1846. function hook_image_toolkits() {
  1847. return array(
  1848. 'working' => array(
  1849. 'title' => t('A toolkit that works.'),
  1850. 'available' => TRUE,
  1851. ),
  1852. 'broken' => array(
  1853. 'title' => t('A toolkit that is "broken" and will not be listed.'),
  1854. 'available' => FALSE,
  1855. ),
  1856. );
  1857. }
  1858. /**
  1859. * Alter an email message created with the drupal_mail() function.
  1860. *
  1861. * hook_mail_alter() allows modification of email messages created and sent
  1862. * with drupal_mail(). Usage examples include adding and/or changing message
  1863. * text, message fields, and message headers.
  1864. *
  1865. * Email messages sent using functions other than drupal_mail() will not
  1866. * invoke hook_mail_alter(). For example, a contributed module directly
  1867. * calling the drupal_mail_system()->mail() or PHP mail() function
  1868. * will not invoke this hook. All core modules use drupal_mail() for
  1869. * messaging, it is best practice but not mandatory in contributed modules.
  1870. *
  1871. * @param $message
  1872. * An array containing the message data. Keys in this array include:
  1873. * - 'id':
  1874. * The drupal_mail() id of the message. Look at module source code or
  1875. * drupal_mail() for possible id values.
  1876. * - 'to':
  1877. * The address or addresses the message will be sent to. The formatting of
  1878. * this string will be validated with the
  1879. * @link http://php.net/manual/filter.filters.validate.php PHP e-mail validation filter. @endlink
  1880. * - 'from':
  1881. * The address the message will be marked as being from, which is
  1882. * either a custom address or the site-wide default email address.
  1883. * - 'subject':
  1884. * Subject of the email to be sent. This must not contain any newline
  1885. * characters, or the email may not be sent properly.
  1886. * - 'body':
  1887. * An array of strings containing the message text. The message body is
  1888. * created by concatenating the individual array strings into a single text
  1889. * string using "\n\n" as a separator.
  1890. * - 'headers':
  1891. * Associative array containing mail headers, such as From, Sender,
  1892. * MIME-Version, Content-Type, etc.
  1893. * - 'params':
  1894. * An array of optional parameters supplied by the caller of drupal_mail()
  1895. * that is used to build the message before hook_mail_alter() is invoked.
  1896. * - 'language':
  1897. * The language object used to build the message before hook_mail_alter()
  1898. * is invoked.
  1899. * - 'send':
  1900. * Set to FALSE to abort sending this email message.
  1901. *
  1902. * @see drupal_mail()
  1903. */
  1904. function hook_mail_alter(&$message) {
  1905. if ($message['id'] == 'modulename_messagekey') {
  1906. if (!example_notifications_optin($message['to'], $message['id'])) {
  1907. // If the recipient has opted to not receive such messages, cancel
  1908. // sending.
  1909. $message['send'] = FALSE;
  1910. return;
  1911. }
  1912. $message['body'][] = "--\nMail sent out from " . variable_get('site_name', t('Drupal'));
  1913. }
  1914. }
  1915. /**
  1916. * Alter the registry of modules implementing a hook.
  1917. *
  1918. * This hook is invoked during module_implements(). A module may implement this
  1919. * hook in order to reorder the implementing modules, which are otherwise
  1920. * ordered by the module's system weight.
  1921. *
  1922. * Note that hooks invoked using drupal_alter() can have multiple variations
  1923. * (such as hook_form_alter() and hook_form_FORM_ID_alter()). drupal_alter()
  1924. * will call all such variants defined by a single module in turn. For the
  1925. * purposes of hook_module_implements_alter(), these variants are treated as
  1926. * a single hook. Thus, to ensure that your implementation of
  1927. * hook_form_FORM_ID_alter() is called at the right time, you will have to
  1928. * change the order of hook_form_alter() implementation in
  1929. * hook_module_implements_alter().
  1930. *
  1931. * @param $implementations
  1932. * An array keyed by the module's name. The value of each item corresponds
  1933. * to a $group, which is usually FALSE, unless the implementation is in a
  1934. * file named $module.$group.inc.
  1935. * @param $hook
  1936. * The name of the module hook being implemented.
  1937. */
  1938. function hook_module_implements_alter(&$implementations, $hook) {
  1939. if ($hook == 'rdf_mapping') {
  1940. // Move my_module_rdf_mapping() to the end of the list. module_implements()
  1941. // iterates through $implementations with a foreach loop which PHP iterates
  1942. // in the order that the items were added, so to move an item to the end of
  1943. // the array, we remove it and then add it.
  1944. $group = $implementations['my_module'];
  1945. unset($implementations['my_module']);
  1946. $implementations['my_module'] = $group;
  1947. }
  1948. }
  1949. /**
  1950. * Return additional themes provided by modules.
  1951. *
  1952. * Only use this hook for testing purposes. Use a hidden MYMODULE_test.module
  1953. * to implement this hook. Testing themes should be hidden, too.
  1954. *
  1955. * This hook is invoked from _system_rebuild_theme_data() and allows modules to
  1956. * register additional themes outside of the regular 'themes' directories of a
  1957. * Drupal installation.
  1958. *
  1959. * @return
  1960. * An associative array. Each key is the system name of a theme and each value
  1961. * is the corresponding path to the theme's .info file.
  1962. */
  1963. function hook_system_theme_info() {
  1964. $themes['mymodule_test_theme'] = drupal_get_path('module', 'mymodule') . '/mymodule_test_theme/mymodule_test_theme.info';
  1965. return $themes;
  1966. }
  1967. /**
  1968. * Alter the information parsed from module and theme .info files
  1969. *
  1970. * This hook is invoked in _system_rebuild_module_data() and in
  1971. * _system_rebuild_theme_data(). A module may implement this hook in order to
  1972. * add to or alter the data generated by reading the .info file with
  1973. * drupal_parse_info_file().
  1974. *
  1975. * @param $info
  1976. * The .info file contents, passed by reference so that it can be altered.
  1977. * @param $file
  1978. * Full information about the module or theme, including $file->name, and
  1979. * $file->filename
  1980. * @param $type
  1981. * Either 'module' or 'theme', depending on the type of .info file that was
  1982. * passed.
  1983. */
  1984. function hook_system_info_alter(&$info, $file, $type) {
  1985. // Only fill this in if the .info file does not define a 'datestamp'.
  1986. if (empty($info['datestamp'])) {
  1987. $info['datestamp'] = filemtime($file->filename);
  1988. }
  1989. }
  1990. /**
  1991. * Define user permissions.
  1992. *
  1993. * This hook can supply permissions that the module defines, so that they
  1994. * can be selected on the user permissions page and used to grant or restrict
  1995. * access to actions the module performs.
  1996. *
  1997. * Permissions are checked using user_access().
  1998. *
  1999. * For a detailed usage example, see page_example.module.
  2000. *
  2001. * @return
  2002. * An array whose keys are permission names and whose corresponding values
  2003. * are arrays containing the following key-value pairs:
  2004. * - title: The human-readable name of the permission, to be shown on the
  2005. * permission administration page. This should be wrapped in the t()
  2006. * function so it can be translated.
  2007. * - description: (optional) A description of what the permission does. This
  2008. * should be wrapped in the t() function so it can be translated.
  2009. * - restrict access: (optional) A boolean which can be set to TRUE to
  2010. * indicate that site administrators should restrict access to this
  2011. * permission to trusted users. This should be used for permissions that
  2012. * have inherent security risks across a variety of potential use cases
  2013. * (for example, the "administer filters" and "bypass node access"
  2014. * permissions provided by Drupal core). When set to TRUE, a standard
  2015. * warning message defined in user_admin_permissions() and output via
  2016. * theme_user_permission_description() will be associated with the
  2017. * permission and displayed with it on the permission administration page.
  2018. * Defaults to FALSE.
  2019. * - warning: (optional) A translated warning message to display for this
  2020. * permission on the permission administration page. This warning overrides
  2021. * the automatic warning generated by 'restrict access' being set to TRUE.
  2022. * This should rarely be used, since it is important for all permissions to
  2023. * have a clear, consistent security warning that is the same across the
  2024. * site. Use the 'description' key instead to provide any information that
  2025. * is specific to the permission you are defining.
  2026. *
  2027. * @see theme_user_permission_description()
  2028. */
  2029. function hook_permission() {
  2030. return array(
  2031. 'administer my module' => array(
  2032. 'title' => t('Administer my module'),
  2033. 'description' => t('Perform administration tasks for my module.'),
  2034. ),
  2035. );
  2036. }
  2037. /**
  2038. * Register a module (or theme's) theme implementations.
  2039. *
  2040. * The implementations declared by this hook have two purposes: either they
  2041. * specify how a particular render array is to be rendered as HTML (this is
  2042. * usually the case if the theme function is assigned to the render array's
  2043. * #theme property), or they return the HTML that should be returned by an
  2044. * invocation of theme(). See
  2045. * @link http://drupal.org/node/933976 Using the theme layer Drupal 7.x @endlink
  2046. * for more information on how to implement theme hooks.
  2047. *
  2048. * The following parameters are all optional.
  2049. *
  2050. * @param array $existing
  2051. * An array of existing implementations that may be used for override
  2052. * purposes. This is primarily useful for themes that may wish to examine
  2053. * existing implementations to extract data (such as arguments) so that
  2054. * it may properly register its own, higher priority implementations.
  2055. * @param $type
  2056. * Whether a theme, module, etc. is being processed. This is primarily useful
  2057. * so that themes tell if they are the actual theme being called or a parent
  2058. * theme. May be one of:
  2059. * - 'module': A module is being checked for theme implementations.
  2060. * - 'base_theme_engine': A theme engine is being checked for a theme that is
  2061. * a parent of the actual theme being used.
  2062. * - 'theme_engine': A theme engine is being checked for the actual theme
  2063. * being used.
  2064. * - 'base_theme': A base theme is being checked for theme implementations.
  2065. * - 'theme': The actual theme in use is being checked.
  2066. * @param $theme
  2067. * The actual name of theme, module, etc. that is being being processed.
  2068. * @param $path
  2069. * The directory path of the theme or module, so that it doesn't need to be
  2070. * looked up.
  2071. *
  2072. * @return array
  2073. * An associative array of theme hook information. The keys on the outer
  2074. * array are the internal names of the hooks, and the values are arrays
  2075. * containing information about the hook. Each information array must contain
  2076. * either a 'variables' element or a 'render element' element, but not both.
  2077. * Use 'render element' if you are theming a single element or element tree
  2078. * composed of elements, such as a form array, a page array, or a single
  2079. * checkbox element. Use 'variables' if your theme implementation is
  2080. * intended to be called directly through theme() and has multiple arguments
  2081. * for the data and style; in this case, the variables not supplied by the
  2082. * calling function will be given default values and passed to the template
  2083. * or theme function. The returned theme information array can contain the
  2084. * following key/value pairs:
  2085. * - variables: (see above) Each array key is the name of the variable, and
  2086. * the value given is used as the default value if the function calling
  2087. * theme() does not supply it. Template implementations receive each array
  2088. * key as a variable in the template file (so they must be legal PHP
  2089. * variable names). Function implementations are passed the variables in a
  2090. * single $variables function argument.
  2091. * - render element: (see above) The name of the renderable element or element
  2092. * tree to pass to the theme function. This name is used as the name of the
  2093. * variable that holds the renderable element or tree in preprocess and
  2094. * process functions.
  2095. * - file: The file the implementation resides in. This file will be included
  2096. * prior to the theme being rendered, to make sure that the function or
  2097. * preprocess function (as needed) is actually loaded; this makes it
  2098. * possible to split theme functions out into separate files quite easily.
  2099. * - path: Override the path of the file to be used. Ordinarily the module or
  2100. * theme path will be used, but if the file will not be in the default
  2101. * path, include it here. This path should be relative to the Drupal root
  2102. * directory.
  2103. * - template: If specified, this theme implementation is a template, and
  2104. * this is the template file without an extension. Do not put .tpl.php on
  2105. * this file; that extension will be added automatically by the default
  2106. * rendering engine (which is PHPTemplate). If 'path', above, is specified,
  2107. * the template should also be in this path.
  2108. * - function: If specified, this will be the function name to invoke for
  2109. * this implementation. If neither 'template' nor 'function' is specified,
  2110. * a default function name will be assumed. For example, if a module
  2111. * registers the 'node' theme hook, 'theme_node' will be assigned to its
  2112. * function. If the chameleon theme registers the node hook, it will be
  2113. * assigned 'chameleon_node' as its function.
  2114. * - base hook: A string declaring the base theme hook if this theme
  2115. * implementation is actually implementing a suggestion for another theme
  2116. * hook.
  2117. * - pattern: A regular expression pattern to be used to allow this theme
  2118. * implementation to have a dynamic name. The convention is to use __ to
  2119. * differentiate the dynamic portion of the theme. For example, to allow
  2120. * forums to be themed individually, the pattern might be: 'forum__'. Then,
  2121. * when the forum is themed, call:
  2122. * @code
  2123. * theme(array('forum__' . $tid, 'forum'), $forum)
  2124. * @endcode
  2125. * - preprocess functions: A list of functions used to preprocess this data.
  2126. * Ordinarily this won't be used; it's automatically filled in. By default,
  2127. * for a module this will be filled in as template_preprocess_HOOK. For
  2128. * a theme this will be filled in as phptemplate_preprocess and
  2129. * phptemplate_preprocess_HOOK as well as themename_preprocess and
  2130. * themename_preprocess_HOOK.
  2131. * - override preprocess functions: Set to TRUE when a theme does NOT want
  2132. * the standard preprocess functions to run. This can be used to give a
  2133. * theme FULL control over how variables are set. For example, if a theme
  2134. * wants total control over how certain variables in the page.tpl.php are
  2135. * set, this can be set to true. Please keep in mind that when this is used
  2136. * by a theme, that theme becomes responsible for making sure necessary
  2137. * variables are set.
  2138. * - type: (automatically derived) Where the theme hook is defined:
  2139. * 'module', 'theme_engine', or 'theme'.
  2140. * - theme path: (automatically derived) The directory path of the theme or
  2141. * module, so that it doesn't need to be looked up.
  2142. *
  2143. * @see hook_theme_registry_alter()
  2144. */
  2145. function hook_theme($existing, $type, $theme, $path) {
  2146. return array(
  2147. 'forum_display' => array(
  2148. 'variables' => array('forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  2149. ),
  2150. 'forum_list' => array(
  2151. 'variables' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
  2152. ),
  2153. 'forum_topic_list' => array(
  2154. 'variables' => array('tid' => NULL, 'topics' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  2155. ),
  2156. 'forum_icon' => array(
  2157. 'variables' => array('new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0),
  2158. ),
  2159. 'status_report' => array(
  2160. 'render element' => 'requirements',
  2161. 'file' => 'system.admin.inc',
  2162. ),
  2163. 'system_date_time_settings' => array(
  2164. 'render element' => 'form',
  2165. 'file' => 'system.admin.inc',
  2166. ),
  2167. );
  2168. }
  2169. /**
  2170. * Alter the theme registry information returned from hook_theme().
  2171. *
  2172. * The theme registry stores information about all available theme hooks,
  2173. * including which callback functions those hooks will call when triggered,
  2174. * what template files are exposed by these hooks, and so on.
  2175. *
  2176. * Note that this hook is only executed as the theme cache is re-built.
  2177. * Changes here will not be visible until the next cache clear.
  2178. *
  2179. * The $theme_registry array is keyed by theme hook name, and contains the
  2180. * information returned from hook_theme(), as well as additional properties
  2181. * added by _theme_process_registry().
  2182. *
  2183. * For example:
  2184. * @code
  2185. * $theme_registry['user_profile'] = array(
  2186. * 'variables' => array(
  2187. * 'account' => NULL,
  2188. * ),
  2189. * 'template' => 'modules/user/user-profile',
  2190. * 'file' => 'modules/user/user.pages.inc',
  2191. * 'type' => 'module',
  2192. * 'theme path' => 'modules/user',
  2193. * 'preprocess functions' => array(
  2194. * 0 => 'template_preprocess',
  2195. * 1 => 'template_preprocess_user_profile',
  2196. * ),
  2197. * );
  2198. * @endcode
  2199. *
  2200. * @param $theme_registry
  2201. * The entire cache of theme registry information, post-processing.
  2202. *
  2203. * @see hook_theme()
  2204. * @see _theme_process_registry()
  2205. */
  2206. function hook_theme_registry_alter(&$theme_registry) {
  2207. // Kill the next/previous forum topic navigation links.
  2208. foreach ($theme_registry['forum_topic_navigation']['preprocess functions'] as $key => $value) {
  2209. if ($value == 'template_preprocess_forum_topic_navigation') {
  2210. unset($theme_registry['forum_topic_navigation']['preprocess functions'][$key]);
  2211. }
  2212. }
  2213. }
  2214. /**
  2215. * Return the machine-readable name of the theme to use for the current page.
  2216. *
  2217. * This hook can be used to dynamically set the theme for the current page
  2218. * request. It should be used by modules which need to override the theme
  2219. * based on dynamic conditions (for example, a module which allows the theme to
  2220. * be set based on the current user's role). The return value of this hook will
  2221. * be used on all pages except those which have a valid per-page or per-section
  2222. * theme set via a theme callback function in hook_menu(); the themes on those
  2223. * pages can only be overridden using hook_menu_alter().
  2224. *
  2225. * Note that returning different themes for the same path may not work with page
  2226. * caching. This is most likely to be a problem if an anonymous user on a given
  2227. * path could have different themes returned under different conditions.
  2228. *
  2229. * Since only one theme can be used at a time, the last (i.e., highest
  2230. * weighted) module which returns a valid theme name from this hook will
  2231. * prevail.
  2232. *
  2233. * @return
  2234. * The machine-readable name of the theme that should be used for the current
  2235. * page request. The value returned from this function will only have an
  2236. * effect if it corresponds to a currently-active theme on the site. Do not
  2237. * return a value if you do not wish to set a custom theme.
  2238. */
  2239. function hook_custom_theme() {
  2240. // Allow the user to request a particular theme via a query parameter.
  2241. if (isset($_GET['theme'])) {
  2242. return $_GET['theme'];
  2243. }
  2244. }
  2245. /**
  2246. * Register XML-RPC callbacks.
  2247. *
  2248. * This hook lets a module register callback functions to be called when
  2249. * particular XML-RPC methods are invoked by a client.
  2250. *
  2251. * @return
  2252. * An array which maps XML-RPC methods to Drupal functions. Each array
  2253. * element is either a pair of method => function or an array with four
  2254. * entries:
  2255. * - The XML-RPC method name (for example, module.function).
  2256. * - The Drupal callback function (for example, module_function).
  2257. * - The method signature is an array of XML-RPC types. The first element
  2258. * of this array is the type of return value and then you should write a
  2259. * list of the types of the parameters. XML-RPC types are the following
  2260. * (See the types at http://www.xmlrpc.com/spec):
  2261. * - "boolean": 0 (false) or 1 (true).
  2262. * - "double": a floating point number (for example, -12.214).
  2263. * - "int": a integer number (for example, -12).
  2264. * - "array": an array without keys (for example, array(1, 2, 3)).
  2265. * - "struct": an associative array or an object (for example,
  2266. * array('one' => 1, 'two' => 2)).
  2267. * - "date": when you return a date, then you may either return a
  2268. * timestamp (time(), mktime() etc.) or an ISO8601 timestamp. When
  2269. * date is specified as an input parameter, then you get an object,
  2270. * which is described in the function xmlrpc_date
  2271. * - "base64": a string containing binary data, automatically
  2272. * encoded/decoded automatically.
  2273. * - "string": anything else, typically a string.
  2274. * - A descriptive help string, enclosed in a t() function for translation
  2275. * purposes.
  2276. * Both forms are shown in the example.
  2277. */
  2278. function hook_xmlrpc() {
  2279. return array(
  2280. 'drupal.login' => 'drupal_login',
  2281. array(
  2282. 'drupal.site.ping',
  2283. 'drupal_directory_ping',
  2284. array('boolean', 'string', 'string', 'string', 'string', 'string'),
  2285. t('Handling ping request'))
  2286. );
  2287. }
  2288. /**
  2289. * Alters the definition of XML-RPC methods before they are called.
  2290. *
  2291. * This hook allows modules to modify the callback definition of declared
  2292. * XML-RPC methods, right before they are invoked by a client. Methods may be
  2293. * added, or existing methods may be altered.
  2294. *
  2295. * Note that hook_xmlrpc() supports two distinct and incompatible formats to
  2296. * define a callback, so care must be taken when altering other methods.
  2297. *
  2298. * @param $methods
  2299. * An asssociative array of method callback definitions, as returned from
  2300. * hook_xmlrpc() implementations.
  2301. *
  2302. * @see hook_xmlrpc()
  2303. * @see xmlrpc_server()
  2304. */
  2305. function hook_xmlrpc_alter(&$methods) {
  2306. // Directly change a simple method.
  2307. $methods['drupal.login'] = 'mymodule_login';
  2308. // Alter complex definitions.
  2309. foreach ($methods as $key => &$method) {
  2310. // Skip simple method definitions.
  2311. if (!is_int($key)) {
  2312. continue;
  2313. }
  2314. // Perform the wanted manipulation.
  2315. if ($method[0] == 'drupal.site.ping') {
  2316. $method[1] = 'mymodule_directory_ping';
  2317. }
  2318. }
  2319. }
  2320. /**
  2321. * Log an event message.
  2322. *
  2323. * This hook allows modules to route log events to custom destinations, such as
  2324. * SMS, Email, pager, syslog, ...etc.
  2325. *
  2326. * @param $log_entry
  2327. * An associative array containing the following keys:
  2328. * - type: The type of message for this entry.
  2329. * - user: The user object for the user who was logged in when the event
  2330. * happened.
  2331. * - uid: The user ID for the user who was logged in when the event happened.
  2332. * - request_uri: The request URI for the page the event happened in.
  2333. * - referer: The page that referred the user to the page where the event
  2334. * occurred.
  2335. * - ip: The IP address where the request for the page came from.
  2336. * - timestamp: The UNIX timestamp of the date/time the event occurred.
  2337. * - severity: The severity of the message; one of the following values as
  2338. * defined in @link http://www.faqs.org/rfcs/rfc3164.html RFC 3164: @endlink
  2339. * - WATCHDOG_EMERGENCY: Emergency, system is unusable.
  2340. * - WATCHDOG_ALERT: Alert, action must be taken immediately.
  2341. * - WATCHDOG_CRITICAL: Critical conditions.
  2342. * - WATCHDOG_ERROR: Error conditions.
  2343. * - WATCHDOG_WARNING: Warning conditions.
  2344. * - WATCHDOG_NOTICE: Normal but significant conditions.
  2345. * - WATCHDOG_INFO: Informational messages.
  2346. * - WATCHDOG_DEBUG: Debug-level messages.
  2347. * - link: An optional link provided by the module that called the watchdog()
  2348. * function.
  2349. * - message: The text of the message to be logged. Variables in the message
  2350. * are indicated by using placeholder strings alongside the variables
  2351. * argument to declare the value of the placeholders. See t() for
  2352. * documentation on how the message and variable parameters interact.
  2353. * - variables: An array of variables to be inserted into the message on
  2354. * display. Will be NULL or missing if a message is already translated or if
  2355. * the message is not possible to translate.
  2356. */
  2357. function hook_watchdog(array $log_entry) {
  2358. global $base_url, $language;
  2359. $severity_list = array(
  2360. WATCHDOG_EMERGENCY => t('Emergency'),
  2361. WATCHDOG_ALERT => t('Alert'),
  2362. WATCHDOG_CRITICAL => t('Critical'),
  2363. WATCHDOG_ERROR => t('Error'),
  2364. WATCHDOG_WARNING => t('Warning'),
  2365. WATCHDOG_NOTICE => t('Notice'),
  2366. WATCHDOG_INFO => t('Info'),
  2367. WATCHDOG_DEBUG => t('Debug'),
  2368. );
  2369. $to = 'someone@example.com';
  2370. $params = array();
  2371. $params['subject'] = t('[@site_name] @severity_desc: Alert from your web site', array(
  2372. '@site_name' => variable_get('site_name', 'Drupal'),
  2373. '@severity_desc' => $severity_list[$log_entry['severity']],
  2374. ));
  2375. $params['message'] = "\nSite: @base_url";
  2376. $params['message'] .= "\nSeverity: (@severity) @severity_desc";
  2377. $params['message'] .= "\nTimestamp: @timestamp";
  2378. $params['message'] .= "\nType: @type";
  2379. $params['message'] .= "\nIP Address: @ip";
  2380. $params['message'] .= "\nRequest URI: @request_uri";
  2381. $params['message'] .= "\nReferrer URI: @referer_uri";
  2382. $params['message'] .= "\nUser: (@uid) @name";
  2383. $params['message'] .= "\nLink: @link";
  2384. $params['message'] .= "\nMessage: \n\n@message";
  2385. $params['message'] = t($params['message'], array(
  2386. '@base_url' => $base_url,
  2387. '@severity' => $log_entry['severity'],
  2388. '@severity_desc' => $severity_list[$log_entry['severity']],
  2389. '@timestamp' => format_date($log_entry['timestamp']),
  2390. '@type' => $log_entry['type'],
  2391. '@ip' => $log_entry['ip'],
  2392. '@request_uri' => $log_entry['request_uri'],
  2393. '@referer_uri' => $log_entry['referer'],
  2394. '@uid' => $log_entry['uid'],
  2395. '@name' => $log_entry['user']->name,
  2396. '@link' => strip_tags($log_entry['link']),
  2397. '@message' => strip_tags($log_entry['message']),
  2398. ));
  2399. drupal_mail('emaillog', 'entry', $to, $language, $params);
  2400. }
  2401. /**
  2402. * Prepare a message based on parameters; called from drupal_mail().
  2403. *
  2404. * Note that hook_mail(), unlike hook_mail_alter(), is only called on the
  2405. * $module argument to drupal_mail(), not all modules.
  2406. *
  2407. * @param $key
  2408. * An identifier of the mail.
  2409. * @param $message
  2410. * An array to be filled in. Elements in this array include:
  2411. * - id: An ID to identify the mail sent. Look at module source code
  2412. * or drupal_mail() for possible id values.
  2413. * - to: The address or addresses the message will be sent to. The formatting
  2414. * of this string will be validated with the
  2415. * @link http://php.net/manual/filter.filters.validate.php PHP e-mail validation filter. @endlink
  2416. * - subject: Subject of the e-mail to be sent. This must not contain any
  2417. * newline characters, or the mail may not be sent properly. drupal_mail()
  2418. * sets this to an empty string when the hook is invoked.
  2419. * - body: An array of lines containing the message to be sent. Drupal will
  2420. * format the correct line endings for you. drupal_mail() sets this to an
  2421. * empty array when the hook is invoked.
  2422. * - from: The address the message will be marked as being from, which is
  2423. * set by drupal_mail() to either a custom address or the site-wide
  2424. * default email address when the hook is invoked.
  2425. * - headers: Associative array containing mail headers, such as From,
  2426. * Sender, MIME-Version, Content-Type, etc. drupal_mail() pre-fills
  2427. * several headers in this array.
  2428. * @param $params
  2429. * An array of parameters supplied by the caller of drupal_mail().
  2430. */
  2431. function hook_mail($key, &$message, $params) {
  2432. $account = $params['account'];
  2433. $context = $params['context'];
  2434. $variables = array(
  2435. '%site_name' => variable_get('site_name', 'Drupal'),
  2436. '%username' => format_username($account),
  2437. );
  2438. if ($context['hook'] == 'taxonomy') {
  2439. $entity = $params['entity'];
  2440. $vocabulary = taxonomy_vocabulary_load($entity->vid);
  2441. $variables += array(
  2442. '%term_name' => $entity->name,
  2443. '%term_description' => $entity->description,
  2444. '%term_id' => $entity->tid,
  2445. '%vocabulary_name' => $vocabulary->name,
  2446. '%vocabulary_description' => $vocabulary->description,
  2447. '%vocabulary_id' => $vocabulary->vid,
  2448. );
  2449. }
  2450. // Node-based variable translation is only available if we have a node.
  2451. if (isset($params['node'])) {
  2452. $node = $params['node'];
  2453. $variables += array(
  2454. '%uid' => $node->uid,
  2455. '%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
  2456. '%node_type' => node_type_get_name($node),
  2457. '%title' => $node->title,
  2458. '%teaser' => $node->teaser,
  2459. '%body' => $node->body,
  2460. );
  2461. }
  2462. $subject = strtr($context['subject'], $variables);
  2463. $body = strtr($context['message'], $variables);
  2464. $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
  2465. $message['body'][] = drupal_html_to_text($body);
  2466. }
  2467. /**
  2468. * Add a list of cache tables to be cleared.
  2469. *
  2470. * This hook allows your module to add cache table names to the list of cache
  2471. * tables that will be cleared by the Clear button on the Performance page or
  2472. * whenever drupal_flush_all_caches is invoked.
  2473. *
  2474. * @return
  2475. * An array of cache table names.
  2476. *
  2477. * @see drupal_flush_all_caches()
  2478. */
  2479. function hook_flush_caches() {
  2480. return array('cache_example');
  2481. }
  2482. /**
  2483. * Perform necessary actions after modules are installed.
  2484. *
  2485. * This function differs from hook_install() in that it gives all other modules
  2486. * a chance to perform actions when a module is installed, whereas
  2487. * hook_install() is only called on the module actually being installed. See
  2488. * module_enable() for a detailed description of the order in which install and
  2489. * enable hooks are invoked.
  2490. *
  2491. * @param $modules
  2492. * An array of the modules that were installed.
  2493. *
  2494. * @see module_enable()
  2495. * @see hook_modules_enabled()
  2496. * @see hook_install()
  2497. */
  2498. function hook_modules_installed($modules) {
  2499. if (in_array('lousy_module', $modules)) {
  2500. variable_set('lousy_module_conflicting_variable', FALSE);
  2501. }
  2502. }
  2503. /**
  2504. * Perform necessary actions after modules are enabled.
  2505. *
  2506. * This function differs from hook_enable() in that it gives all other modules a
  2507. * chance to perform actions when modules are enabled, whereas hook_enable() is
  2508. * only called on the module actually being enabled. See module_enable() for a
  2509. * detailed description of the order in which install and enable hooks are
  2510. * invoked.
  2511. *
  2512. * @param $modules
  2513. * An array of the modules that were enabled.
  2514. *
  2515. * @see hook_enable()
  2516. * @see hook_modules_installed()
  2517. * @see module_enable()
  2518. */
  2519. function hook_modules_enabled($modules) {
  2520. if (in_array('lousy_module', $modules)) {
  2521. drupal_set_message(t('mymodule is not compatible with lousy_module'), 'error');
  2522. mymodule_disable_functionality();
  2523. }
  2524. }
  2525. /**
  2526. * Perform necessary actions after modules are disabled.
  2527. *
  2528. * This function differs from hook_disable() in that it gives all other modules
  2529. * a chance to perform actions when modules are disabled, whereas hook_disable()
  2530. * is only called on the module actually being disabled.
  2531. *
  2532. * @param $modules
  2533. * An array of the modules that were disabled.
  2534. *
  2535. * @see hook_disable()
  2536. * @see hook_modules_uninstalled()
  2537. */
  2538. function hook_modules_disabled($modules) {
  2539. if (in_array('lousy_module', $modules)) {
  2540. mymodule_enable_functionality();
  2541. }
  2542. }
  2543. /**
  2544. * Perform necessary actions after modules are uninstalled.
  2545. *
  2546. * This function differs from hook_uninstall() in that it gives all other
  2547. * modules a chance to perform actions when a module is uninstalled, whereas
  2548. * hook_uninstall() is only called on the module actually being uninstalled.
  2549. *
  2550. * It is recommended that you implement this hook if your module stores
  2551. * data that may have been set by other modules.
  2552. *
  2553. * @param $modules
  2554. * An array of the modules that were uninstalled.
  2555. *
  2556. * @see hook_uninstall()
  2557. * @see hook_modules_disabled()
  2558. */
  2559. function hook_modules_uninstalled($modules) {
  2560. foreach ($modules as $module) {
  2561. db_delete('mymodule_table')
  2562. ->condition('module', $module)
  2563. ->execute();
  2564. }
  2565. mymodule_cache_rebuild();
  2566. }
  2567. /**
  2568. * Registers PHP stream wrapper implementations associated with a module.
  2569. *
  2570. * Provide a facility for managing and querying user-defined stream wrappers
  2571. * in PHP. PHP's internal stream_get_wrappers() doesn't return the class
  2572. * registered to handle a stream, which we need to be able to find the handler
  2573. * for class instantiation.
  2574. *
  2575. * If a module registers a scheme that is already registered with PHP, it will
  2576. * be unregistered and replaced with the specified class.
  2577. *
  2578. * @return
  2579. * A nested array, keyed first by scheme name ("public" for "public://"),
  2580. * then keyed by the following values:
  2581. * - 'name' A short string to name the wrapper.
  2582. * - 'class' A string specifying the PHP class that implements the
  2583. * DrupalStreamWrapperInterface interface.
  2584. * - 'description' A string with a short description of what the wrapper does.
  2585. * - 'type' (Optional) A bitmask of flags indicating what type of streams this
  2586. * wrapper will access - local or remote, readable and/or writeable, etc.
  2587. * Many shortcut constants are defined in stream_wrappers.inc. Defaults to
  2588. * STREAM_WRAPPERS_NORMAL which includes all of these bit flags:
  2589. * - STREAM_WRAPPERS_READ
  2590. * - STREAM_WRAPPERS_WRITE
  2591. * - STREAM_WRAPPERS_VISIBLE
  2592. *
  2593. * @see file_get_stream_wrappers()
  2594. * @see hook_stream_wrappers_alter()
  2595. * @see system_stream_wrappers()
  2596. */
  2597. function hook_stream_wrappers() {
  2598. return array(
  2599. 'public' => array(
  2600. 'name' => t('Public files'),
  2601. 'class' => 'DrupalPublicStreamWrapper',
  2602. 'description' => t('Public local files served by the webserver.'),
  2603. 'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
  2604. ),
  2605. 'private' => array(
  2606. 'name' => t('Private files'),
  2607. 'class' => 'DrupalPrivateStreamWrapper',
  2608. 'description' => t('Private local files served by Drupal.'),
  2609. 'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
  2610. ),
  2611. 'temp' => array(
  2612. 'name' => t('Temporary files'),
  2613. 'class' => 'DrupalTempStreamWrapper',
  2614. 'description' => t('Temporary local files for upload and previews.'),
  2615. 'type' => STREAM_WRAPPERS_LOCAL_HIDDEN,
  2616. ),
  2617. 'cdn' => array(
  2618. 'name' => t('Content delivery network files'),
  2619. 'class' => 'MyModuleCDNStreamWrapper',
  2620. 'description' => t('Files served by a content delivery network.'),
  2621. // 'type' can be omitted to use the default of STREAM_WRAPPERS_NORMAL
  2622. ),
  2623. 'youtube' => array(
  2624. 'name' => t('YouTube video'),
  2625. 'class' => 'MyModuleYouTubeStreamWrapper',
  2626. 'description' => t('Video streamed from YouTube.'),
  2627. // A module implementing YouTube integration may decide to support using
  2628. // the YouTube API for uploading video, but here, we assume that this
  2629. // particular module only supports playing YouTube video.
  2630. 'type' => STREAM_WRAPPERS_READ_VISIBLE,
  2631. ),
  2632. );
  2633. }
  2634. /**
  2635. * Alters the list of PHP stream wrapper implementations.
  2636. *
  2637. * @see file_get_stream_wrappers()
  2638. * @see hook_stream_wrappers()
  2639. */
  2640. function hook_stream_wrappers_alter(&$wrappers) {
  2641. // Change the name of private files to reflect the performance.
  2642. $wrappers['private']['name'] = t('Slow files');
  2643. }
  2644. /**
  2645. * Load additional information into file objects.
  2646. *
  2647. * file_load_multiple() calls this hook to allow modules to load
  2648. * additional information into each file.
  2649. *
  2650. * @param $files
  2651. * An array of file objects, indexed by fid.
  2652. *
  2653. * @see file_load_multiple()
  2654. * @see file_load()
  2655. */
  2656. function hook_file_load($files) {
  2657. // Add the upload specific data into the file object.
  2658. $result = db_query('SELECT * FROM {upload} u WHERE u.fid IN (:fids)', array(':fids' => array_keys($files)))->fetchAll(PDO::FETCH_ASSOC);
  2659. foreach ($result as $record) {
  2660. foreach ($record as $key => $value) {
  2661. $files[$record['fid']]->$key = $value;
  2662. }
  2663. }
  2664. }
  2665. /**
  2666. * Check that files meet a given criteria.
  2667. *
  2668. * This hook lets modules perform additional validation on files. They're able
  2669. * to report a failure by returning one or more error messages.
  2670. *
  2671. * @param $file
  2672. * The file object being validated.
  2673. * @return
  2674. * An array of error messages. If there are no problems with the file return
  2675. * an empty array.
  2676. *
  2677. * @see file_validate()
  2678. */
  2679. function hook_file_validate($file) {
  2680. $errors = array();
  2681. if (empty($file->filename)) {
  2682. $errors[] = t("The file's name is empty. Please give a name to the file.");
  2683. }
  2684. if (strlen($file->filename) > 255) {
  2685. $errors[] = t("The file's name exceeds the 255 characters limit. Please rename the file and try again.");
  2686. }
  2687. return $errors;
  2688. }
  2689. /**
  2690. * Act on a file being inserted or updated.
  2691. *
  2692. * This hook is called when a file has been added to the database. The hook
  2693. * doesn't distinguish between files created as a result of a copy or those
  2694. * created by an upload.
  2695. *
  2696. * @param $file
  2697. * The file that has just been created.
  2698. *
  2699. * @see file_save()
  2700. */
  2701. function hook_file_presave($file) {
  2702. // Change the file timestamp to an hour prior.
  2703. $file->timestamp -= 3600;
  2704. }
  2705. /**
  2706. * Respond to a file being added.
  2707. *
  2708. * This hook is called after a file has been added to the database. The hook
  2709. * doesn't distinguish between files created as a result of a copy or those
  2710. * created by an upload.
  2711. *
  2712. * @param $file
  2713. * The file that has been added.
  2714. *
  2715. * @see file_save()
  2716. */
  2717. function hook_file_insert($file) {
  2718. // Add a message to the log, if the file is a jpg
  2719. $validate = file_validate_extensions($file, 'jpg');
  2720. if (empty($validate)) {
  2721. watchdog('file', 'A jpg has been added.');
  2722. }
  2723. }
  2724. /**
  2725. * Respond to a file being updated.
  2726. *
  2727. * This hook is called when file_save() is called on an existing file.
  2728. *
  2729. * @param $file
  2730. * The file that has just been updated.
  2731. *
  2732. * @see file_save()
  2733. */
  2734. function hook_file_update($file) {
  2735. }
  2736. /**
  2737. * Respond to a file that has been copied.
  2738. *
  2739. * @param $file
  2740. * The newly copied file object.
  2741. * @param $source
  2742. * The original file before the copy.
  2743. *
  2744. * @see file_copy()
  2745. */
  2746. function hook_file_copy($file, $source) {
  2747. }
  2748. /**
  2749. * Respond to a file that has been moved.
  2750. *
  2751. * @param $file
  2752. * The updated file object after the move.
  2753. * @param $source
  2754. * The original file object before the move.
  2755. *
  2756. * @see file_move()
  2757. */
  2758. function hook_file_move($file, $source) {
  2759. }
  2760. /**
  2761. * Respond to a file being deleted.
  2762. *
  2763. * @param $file
  2764. * The file that has just been deleted.
  2765. *
  2766. * @see file_delete()
  2767. */
  2768. function hook_file_delete($file) {
  2769. // Delete all information associated with the file.
  2770. db_delete('upload')->condition('fid', $file->fid)->execute();
  2771. }
  2772. /**
  2773. * Control access to private file downloads and specify HTTP headers.
  2774. *
  2775. * This hook allows modules enforce permissions on file downloads when the
  2776. * private file download method is selected. Modules can also provide headers
  2777. * to specify information like the file's name or MIME type.
  2778. *
  2779. * @param $uri
  2780. * The URI of the file.
  2781. * @return
  2782. * If the user does not have permission to access the file, return -1. If the
  2783. * user has permission, return an array with the appropriate headers. If the
  2784. * file is not controlled by the current module, the return value should be
  2785. * NULL.
  2786. *
  2787. * @see file_download()
  2788. */
  2789. function hook_file_download($uri) {
  2790. // Check if the file is controlled by the current module.
  2791. if (!file_prepare_directory($uri)) {
  2792. $uri = FALSE;
  2793. }
  2794. if (strpos(file_uri_target($uri), variable_get('user_picture_path', 'pictures') . '/picture-') === 0) {
  2795. if (!user_access('access user profiles')) {
  2796. // Access to the file is denied.
  2797. return -1;
  2798. }
  2799. else {
  2800. $info = image_get_info($uri);
  2801. return array('Content-Type' => $info['mime_type']);
  2802. }
  2803. }
  2804. }
  2805. /**
  2806. * Alter the URL to a file.
  2807. *
  2808. * This hook is called from file_create_url(), and is called fairly
  2809. * frequently (10+ times per page), depending on how many files there are in a
  2810. * given page.
  2811. * If CSS and JS aggregation are disabled, this can become very frequently
  2812. * (50+ times per page) so performance is critical.
  2813. *
  2814. * This function should alter the URI, if it wants to rewrite the file URL.
  2815. *
  2816. * @param $uri
  2817. * The URI to a file for which we need an external URL, or the path to a
  2818. * shipped file.
  2819. */
  2820. function hook_file_url_alter(&$uri) {
  2821. global $user;
  2822. // User 1 will always see the local file in this example.
  2823. if ($user->uid == 1) {
  2824. return;
  2825. }
  2826. $cdn1 = 'http://cdn1.example.com';
  2827. $cdn2 = 'http://cdn2.example.com';
  2828. $cdn_extensions = array('css', 'js', 'gif', 'jpg', 'jpeg', 'png');
  2829. // Most CDNs don't support private file transfers without a lot of hassle,
  2830. // so don't support this in the common case.
  2831. $schemes = array('public');
  2832. $scheme = file_uri_scheme($uri);
  2833. // Only serve shipped files and public created files from the CDN.
  2834. if (!$scheme || in_array($scheme, $schemes)) {
  2835. // Shipped files.
  2836. if (!$scheme) {
  2837. $path = $uri;
  2838. }
  2839. // Public created files.
  2840. else {
  2841. $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
  2842. $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
  2843. }
  2844. // Clean up Windows paths.
  2845. $path = str_replace('\\', '/', $path);
  2846. // Serve files with one of the CDN extensions from CDN 1, all others from
  2847. // CDN 2.
  2848. $pathinfo = pathinfo($path);
  2849. if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], $cdn_extensions)) {
  2850. $uri = $cdn1 . '/' . $path;
  2851. }
  2852. else {
  2853. $uri = $cdn2 . '/' . $path;
  2854. }
  2855. }
  2856. }
  2857. /**
  2858. * Check installation requirements and do status reporting.
  2859. *
  2860. * This hook has three closely related uses, determined by the $phase argument:
  2861. * - Checking installation requirements ($phase == 'install').
  2862. * - Checking update requirements ($phase == 'update').
  2863. * - Status reporting ($phase == 'runtime').
  2864. *
  2865. * Note that this hook, like all others dealing with installation and updates,
  2866. * must reside in a module_name.install file, or it will not properly abort
  2867. * the installation of the module if a critical requirement is missing.
  2868. *
  2869. * During the 'install' phase, modules can for example assert that
  2870. * library or server versions are available or sufficient.
  2871. * Note that the installation of a module can happen during installation of
  2872. * Drupal itself (by install.php) with an installation profile or later by hand.
  2873. * As a consequence, install-time requirements must be checked without access
  2874. * to the full Drupal API, because it is not available during install.php.
  2875. * For localization you should for example use $t = get_t() to
  2876. * retrieve the appropriate localization function name (t() or st()).
  2877. * If a requirement has a severity of REQUIREMENT_ERROR, install.php will abort
  2878. * or at least the module will not install.
  2879. * Other severity levels have no effect on the installation.
  2880. * Module dependencies do not belong to these installation requirements,
  2881. * but should be defined in the module's .info file.
  2882. *
  2883. * The 'runtime' phase is not limited to pure installation requirements
  2884. * but can also be used for more general status information like maintenance
  2885. * tasks and security issues.
  2886. * The returned 'requirements' will be listed on the status report in the
  2887. * administration section, with indication of the severity level.
  2888. * Moreover, any requirement with a severity of REQUIREMENT_ERROR severity will
  2889. * result in a notice on the administration configuration page.
  2890. *
  2891. * @param $phase
  2892. * The phase in which requirements are checked:
  2893. * - install: The module is being installed.
  2894. * - update: The module is enabled and update.php is run.
  2895. * - runtime: The runtime requirements are being checked and shown on the
  2896. * status report page.
  2897. *
  2898. * @return
  2899. * A keyed array of requirements. Each requirement is itself an array with
  2900. * the following items:
  2901. * - title: The name of the requirement.
  2902. * - value: The current value (e.g., version, time, level, etc). During
  2903. * install phase, this should only be used for version numbers, do not set
  2904. * it if not applicable.
  2905. * - description: The description of the requirement/status.
  2906. * - severity: The requirement's result/severity level, one of:
  2907. * - REQUIREMENT_INFO: For info only.
  2908. * - REQUIREMENT_OK: The requirement is satisfied.
  2909. * - REQUIREMENT_WARNING: The requirement failed with a warning.
  2910. * - REQUIREMENT_ERROR: The requirement failed with an error.
  2911. */
  2912. function hook_requirements($phase) {
  2913. $requirements = array();
  2914. // Ensure translations don't break during installation.
  2915. $t = get_t();
  2916. // Report Drupal version
  2917. if ($phase == 'runtime') {
  2918. $requirements['drupal'] = array(
  2919. 'title' => $t('Drupal'),
  2920. 'value' => VERSION,
  2921. 'severity' => REQUIREMENT_INFO
  2922. );
  2923. }
  2924. // Test PHP version
  2925. $requirements['php'] = array(
  2926. 'title' => $t('PHP'),
  2927. 'value' => ($phase == 'runtime') ? l(phpversion(), 'admin/reports/status/php') : phpversion(),
  2928. );
  2929. if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) {
  2930. $requirements['php']['description'] = $t('Your PHP installation is too old. Drupal requires at least PHP %version.', array('%version' => DRUPAL_MINIMUM_PHP));
  2931. $requirements['php']['severity'] = REQUIREMENT_ERROR;
  2932. }
  2933. // Report cron status
  2934. if ($phase == 'runtime') {
  2935. $cron_last = variable_get('cron_last');
  2936. if (is_numeric($cron_last)) {
  2937. $requirements['cron']['value'] = $t('Last run !time ago', array('!time' => format_interval(REQUEST_TIME - $cron_last)));
  2938. }
  2939. else {
  2940. $requirements['cron'] = array(
  2941. 'description' => $t('Cron has not run. It appears cron jobs have not been setup on your system. Check the help pages for <a href="@url">configuring cron jobs</a>.', array('@url' => 'http://drupal.org/cron')),
  2942. 'severity' => REQUIREMENT_ERROR,
  2943. 'value' => $t('Never run'),
  2944. );
  2945. }
  2946. $requirements['cron']['description'] .= ' ' . $t('You can <a href="@cron">run cron manually</a>.', array('@cron' => url('admin/reports/status/run-cron')));
  2947. $requirements['cron']['title'] = $t('Cron maintenance tasks');
  2948. }
  2949. return $requirements;
  2950. }
  2951. /**
  2952. * Define the current version of the database schema.
  2953. *
  2954. * A Drupal schema definition is an array structure representing one or
  2955. * more tables and their related keys and indexes. A schema is defined by
  2956. * hook_schema() which must live in your module's .install file.
  2957. *
  2958. * This hook is called at install and uninstall time, and in the latter
  2959. * case, it cannot rely on the .module file being loaded or hooks being known.
  2960. * If the .module file is needed, it may be loaded with drupal_load().
  2961. *
  2962. * The tables declared by this hook will be automatically created when
  2963. * the module is first enabled, and removed when the module is uninstalled.
  2964. * This happens before hook_install() is invoked, and after hook_uninstall()
  2965. * is invoked, respectively.
  2966. *
  2967. * By declaring the tables used by your module via an implementation of
  2968. * hook_schema(), these tables will be available on all supported database
  2969. * engines. You don't have to deal with the different SQL dialects for table
  2970. * creation and alteration of the supported database engines.
  2971. *
  2972. * See the Schema API Handbook at http://drupal.org/node/146843 for
  2973. * details on schema definition structures.
  2974. *
  2975. * @return
  2976. * A schema definition structure array. For each element of the
  2977. * array, the key is a table name and the value is a table structure
  2978. * definition.
  2979. *
  2980. * @ingroup schemaapi
  2981. */
  2982. function hook_schema() {
  2983. $schema['node'] = array(
  2984. // example (partial) specification for table "node"
  2985. 'description' => 'The base table for nodes.',
  2986. 'fields' => array(
  2987. 'nid' => array(
  2988. 'description' => 'The primary identifier for a node.',
  2989. 'type' => 'serial',
  2990. 'unsigned' => TRUE,
  2991. 'not null' => TRUE,
  2992. ),
  2993. 'vid' => array(
  2994. 'description' => 'The current {node_revision}.vid version identifier.',
  2995. 'type' => 'int',
  2996. 'unsigned' => TRUE,
  2997. 'not null' => TRUE,
  2998. 'default' => 0,
  2999. ),
  3000. 'type' => array(
  3001. 'description' => 'The {node_type} of this node.',
  3002. 'type' => 'varchar',
  3003. 'length' => 32,
  3004. 'not null' => TRUE,
  3005. 'default' => '',
  3006. ),
  3007. 'title' => array(
  3008. 'description' => 'The title of this node, always treated as non-markup plain text.',
  3009. 'type' => 'varchar',
  3010. 'length' => 255,
  3011. 'not null' => TRUE,
  3012. 'default' => '',
  3013. ),
  3014. ),
  3015. 'indexes' => array(
  3016. 'node_changed' => array('changed'),
  3017. 'node_created' => array('created'),
  3018. ),
  3019. 'unique keys' => array(
  3020. 'nid_vid' => array('nid', 'vid'),
  3021. 'vid' => array('vid'),
  3022. ),
  3023. 'foreign keys' => array(
  3024. 'node_revision' => array(
  3025. 'table' => 'node_revision',
  3026. 'columns' => array('vid' => 'vid'),
  3027. ),
  3028. 'node_author' => array(
  3029. 'table' => 'users',
  3030. 'columns' => array('uid' => 'uid'),
  3031. ),
  3032. ),
  3033. 'primary key' => array('nid'),
  3034. );
  3035. return $schema;
  3036. }
  3037. /**
  3038. * Perform alterations to existing database schemas.
  3039. *
  3040. * When a module modifies the database structure of another module (by
  3041. * changing, adding or removing fields, keys or indexes), it should
  3042. * implement hook_schema_alter() to update the default $schema to take its
  3043. * changes into account.
  3044. *
  3045. * See hook_schema() for details on the schema definition structure.
  3046. *
  3047. * @param $schema
  3048. * Nested array describing the schemas for all modules.
  3049. */
  3050. function hook_schema_alter(&$schema) {
  3051. // Add field to existing schema.
  3052. $schema['users']['fields']['timezone_id'] = array(
  3053. 'type' => 'int',
  3054. 'not null' => TRUE,
  3055. 'default' => 0,
  3056. 'description' => 'Per-user timezone configuration.',
  3057. );
  3058. }
  3059. /**
  3060. * Perform alterations to a structured query.
  3061. *
  3062. * Structured (aka dynamic) queries that have tags associated may be altered by any module
  3063. * before the query is executed.
  3064. *
  3065. * @param $query
  3066. * A Query object describing the composite parts of a SQL query.
  3067. *
  3068. * @see hook_query_TAG_alter()
  3069. * @see node_query_node_access_alter()
  3070. * @see QueryAlterableInterface
  3071. * @see SelectQueryInterface
  3072. */
  3073. function hook_query_alter(QueryAlterableInterface $query) {
  3074. if ($query->hasTag('micro_limit')) {
  3075. $query->range(0, 2);
  3076. }
  3077. }
  3078. /**
  3079. * Perform alterations to a structured query for a given tag.
  3080. *
  3081. * @param $query
  3082. * An Query object describing the composite parts of a SQL query.
  3083. *
  3084. * @see hook_query_alter()
  3085. * @see node_query_node_access_alter()
  3086. * @see QueryAlterableInterface
  3087. * @see SelectQueryInterface
  3088. */
  3089. function hook_query_TAG_alter(QueryAlterableInterface $query) {
  3090. // Skip the extra expensive alterations if site has no node access control modules.
  3091. if (!node_access_view_all_nodes()) {
  3092. // Prevent duplicates records.
  3093. $query->distinct();
  3094. // The recognized operations are 'view', 'update', 'delete'.
  3095. if (!$op = $query->getMetaData('op')) {
  3096. $op = 'view';
  3097. }
  3098. // Skip the extra joins and conditions for node admins.
  3099. if (!user_access('bypass node access')) {
  3100. // The node_access table has the access grants for any given node.
  3101. $access_alias = $query->join('node_access', 'na', '%alias.nid = n.nid');
  3102. $or = db_or();
  3103. // If any grant exists for the specified user, then user has access to the node for the specified operation.
  3104. foreach (node_access_grants($op, $query->getMetaData('account')) as $realm => $gids) {
  3105. foreach ($gids as $gid) {
  3106. $or->condition(db_and()
  3107. ->condition($access_alias . '.gid', $gid)
  3108. ->condition($access_alias . '.realm', $realm)
  3109. );
  3110. }
  3111. }
  3112. if (count($or->conditions())) {
  3113. $query->condition($or);
  3114. }
  3115. $query->condition($access_alias . 'grant_' . $op, 1, '>=');
  3116. }
  3117. }
  3118. }
  3119. /**
  3120. * Perform setup tasks when the module is installed.
  3121. *
  3122. * If the module implements hook_schema(), the database tables will
  3123. * be created before this hook is fired.
  3124. *
  3125. * Implementations of this hook are by convention declared in the module's
  3126. * .install file. The implementation can rely on the .module file being loaded.
  3127. * The hook will only be called the first time a module is enabled or after it
  3128. * is re-enabled after being uninstalled. The module's schema version will be
  3129. * set to the module's greatest numbered update hook. Because of this, any time
  3130. * a hook_update_N() is added to the module, this function needs to be updated
  3131. * to reflect the current version of the database schema.
  3132. *
  3133. * See the @link http://drupal.org/node/146843 Schema API documentation @endlink
  3134. * for details on hook_schema and how database tables are defined.
  3135. *
  3136. * Note that since this function is called from a full bootstrap, all functions
  3137. * (including those in modules enabled by the current page request) are
  3138. * available when this hook is called. Use cases could be displaying a user
  3139. * message, or calling a module function necessary for initial setup, etc.
  3140. *
  3141. * Please be sure that anything added or modified in this function that can
  3142. * be removed during uninstall should be removed with hook_uninstall().
  3143. *
  3144. * @see hook_schema()
  3145. * @see module_enable()
  3146. * @see hook_enable()
  3147. * @see hook_disable()
  3148. * @see hook_uninstall()
  3149. * @see hook_modules_installed()
  3150. */
  3151. function hook_install() {
  3152. // Populate the default {node_access} record.
  3153. db_insert('node_access')
  3154. ->fields(array(
  3155. 'nid' => 0,
  3156. 'gid' => 0,
  3157. 'realm' => 'all',
  3158. 'grant_view' => 1,
  3159. 'grant_update' => 0,
  3160. 'grant_delete' => 0,
  3161. ))
  3162. ->execute();
  3163. }
  3164. /**
  3165. * Perform a single update.
  3166. *
  3167. * For each change that requires one or more actions to be performed when
  3168. * updating a site, add a new hook_update_N(), which will be called by
  3169. * update.php. The documentation block preceding this function is stripped of
  3170. * newlines and used as the description for the update on the pending updates
  3171. * task list. Schema updates should adhere to the
  3172. * @link http://drupal.org/node/150215 Schema API. @endlink
  3173. *
  3174. * Implementations of hook_update_N() are named (module name)_update_(number).
  3175. * The numbers are composed of three parts:
  3176. * - 1 digit for Drupal core compatibility.
  3177. * - 1 digit for your module's major release version (e.g., is this the 7.x-1.*
  3178. * (1) or 7.x-2.* (2) series of your module?). This digit should be 0 for
  3179. * initial porting of your module to a new Drupal core API.
  3180. * - 2 digits for sequential counting, starting with 00.
  3181. *
  3182. * Examples:
  3183. * - mymodule_update_7000(): This is the required update for mymodule to run
  3184. * with Drupal core API 7.x when upgrading from Drupal core API 6.x.
  3185. * - mymodule_update_7100(): This is the first update to get the database ready
  3186. * to run mymodule 7.x-1.*.
  3187. * - mymodule_update_7200(): This is the first update to get the database ready
  3188. * to run mymodule 7.x-2.*. Users can directly update from 6.x-2.* to 7.x-2.*
  3189. * and they get all 70xx and 72xx updates, but not 71xx updates, because
  3190. * those reside in the 7.x-1.x branch only.
  3191. *
  3192. * A good rule of thumb is to remove updates older than two major releases of
  3193. * Drupal. See hook_update_last_removed() to notify Drupal about the removals.
  3194. * For further information about releases and release numbers see:
  3195. * @link http://drupal.org/node/711070 Maintaining a drupal.org project with Git @endlink
  3196. *
  3197. * Never renumber update functions.
  3198. *
  3199. * Implementations of this hook should be placed in a mymodule.install file in
  3200. * the same directory as mymodule.module. Drupal core's updates are implemented
  3201. * using the system module as a name and stored in database/updates.inc.
  3202. *
  3203. * Not all module functions are available from within a hook_update_N() function.
  3204. * In order to call a function from your mymodule.module or an include file,
  3205. * you need to explicitly load that file first.
  3206. *
  3207. * During database updates the schema of any module could be out of date. For
  3208. * this reason, caution is needed when using any API function within an update
  3209. * function - particularly CRUD functions, functions that depend on the schema
  3210. * (for example by using drupal_write_record()), and any functions that invoke
  3211. * hooks. See @link update_api Update versions of API functions @endlink for
  3212. * details.
  3213. *
  3214. * If your update task is potentially time-consuming, you'll need to implement a
  3215. * multipass update to avoid PHP timeouts. Multipass updates use the $sandbox
  3216. * parameter provided by the batch API (normally, $context['sandbox']) to store
  3217. * information between successive calls, and the $sandbox['#finished'] value
  3218. * to provide feedback regarding completion level.
  3219. *
  3220. * See the batch operations page for more information on how to use the
  3221. * @link http://drupal.org/node/180528 Batch API. @endlink
  3222. *
  3223. * @param $sandbox
  3224. * Stores information for multipass updates. See above for more information.
  3225. *
  3226. * @throws DrupalUpdateException, PDOException
  3227. * In case of error, update hooks should throw an instance of DrupalUpdateException
  3228. * with a meaningful message for the user. If a database query fails for whatever
  3229. * reason, it will throw a PDOException.
  3230. *
  3231. * @return
  3232. * Optionally, update hooks may return a translated string that will be
  3233. * displayed to the user after the update has completed. If no message is
  3234. * returned, no message will be presented to the user.
  3235. *
  3236. * @see batch
  3237. * @see schemaapi
  3238. * @see update_api
  3239. * @see hook_update_last_removed()
  3240. * @see update_get_update_list()
  3241. */
  3242. function hook_update_N(&$sandbox) {
  3243. // For non-multipass updates, the signature can simply be;
  3244. // function hook_update_N() {
  3245. // For most updates, the following is sufficient.
  3246. db_add_field('mytable1', 'newcol', array('type' => 'int', 'not null' => TRUE, 'description' => 'My new integer column.'));
  3247. // However, for more complex operations that may take a long time,
  3248. // you may hook into Batch API as in the following example.
  3249. // Update 3 users at a time to have an exclamation point after their names.
  3250. // (They're really happy that we can do batch API in this hook!)
  3251. if (!isset($sandbox['progress'])) {
  3252. $sandbox['progress'] = 0;
  3253. $sandbox['current_uid'] = 0;
  3254. // We'll -1 to disregard the uid 0...
  3255. $sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')->fetchField() - 1;
  3256. }
  3257. $users = db_select('users', 'u')
  3258. ->fields('u', array('uid', 'name'))
  3259. ->condition('uid', $sandbox['current_uid'], '>')
  3260. ->range(0, 3)
  3261. ->orderBy('uid', 'ASC')
  3262. ->execute();
  3263. foreach ($users as $user) {
  3264. $user->name .= '!';
  3265. db_update('users')
  3266. ->fields(array('name' => $user->name))
  3267. ->condition('uid', $user->uid)
  3268. ->execute();
  3269. $sandbox['progress']++;
  3270. $sandbox['current_uid'] = $user->uid;
  3271. }
  3272. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  3273. // To display a message to the user when the update is completed, return it.
  3274. // If you do not want to display a completion message, simply return nothing.
  3275. return t('The update did what it was supposed to do.');
  3276. // In case of an error, simply throw an exception with an error message.
  3277. throw new DrupalUpdateException('Something went wrong; here is what you should do.');
  3278. }
  3279. /**
  3280. * Return an array of information about module update dependencies.
  3281. *
  3282. * This can be used to indicate update functions from other modules that your
  3283. * module's update functions depend on, or vice versa. It is used by the update
  3284. * system to determine the appropriate order in which updates should be run, as
  3285. * well as to search for missing dependencies.
  3286. *
  3287. * Implementations of this hook should be placed in a mymodule.install file in
  3288. * the same directory as mymodule.module.
  3289. *
  3290. * @return
  3291. * A multidimensional array containing information about the module update
  3292. * dependencies. The first two levels of keys represent the module and update
  3293. * number (respectively) for which information is being returned, and the
  3294. * value is an array of information about that update's dependencies. Within
  3295. * this array, each key represents a module, and each value represents the
  3296. * number of an update function within that module. In the event that your
  3297. * update function depends on more than one update from a particular module,
  3298. * you should always list the highest numbered one here (since updates within
  3299. * a given module always run in numerical order).
  3300. *
  3301. * @see update_resolve_dependencies()
  3302. * @see hook_update_N()
  3303. */
  3304. function hook_update_dependencies() {
  3305. // Indicate that the mymodule_update_7000() function provided by this module
  3306. // must run after the another_module_update_7002() function provided by the
  3307. // 'another_module' module.
  3308. $dependencies['mymodule'][7000] = array(
  3309. 'another_module' => 7002,
  3310. );
  3311. // Indicate that the mymodule_update_7001() function provided by this module
  3312. // must run before the yet_another_module_update_7004() function provided by
  3313. // the 'yet_another_module' module. (Note that declaring dependencies in this
  3314. // direction should be done only in rare situations, since it can lead to the
  3315. // following problem: If a site has already run the yet_another_module
  3316. // module's database updates before it updates its codebase to pick up the
  3317. // newest mymodule code, then the dependency declared here will be ignored.)
  3318. $dependencies['yet_another_module'][7004] = array(
  3319. 'mymodule' => 7001,
  3320. );
  3321. return $dependencies;
  3322. }
  3323. /**
  3324. * Return a number which is no longer available as hook_update_N().
  3325. *
  3326. * If you remove some update functions from your mymodule.install file, you
  3327. * should notify Drupal of those missing functions. This way, Drupal can
  3328. * ensure that no update is accidentally skipped.
  3329. *
  3330. * Implementations of this hook should be placed in a mymodule.install file in
  3331. * the same directory as mymodule.module.
  3332. *
  3333. * @return
  3334. * An integer, corresponding to hook_update_N() which has been removed from
  3335. * mymodule.install.
  3336. *
  3337. * @see hook_update_N()
  3338. */
  3339. function hook_update_last_removed() {
  3340. // We've removed the 5.x-1.x version of mymodule, including database updates.
  3341. // The next update function is mymodule_update_5200().
  3342. return 5103;
  3343. }
  3344. /**
  3345. * Remove any information that the module sets.
  3346. *
  3347. * The information that the module should remove includes:
  3348. * - variables that the module has set using variable_set() or system_settings_form()
  3349. * - modifications to existing tables
  3350. *
  3351. * The module should not remove its entry from the {system} table. Database
  3352. * tables defined by hook_schema() will be removed automatically.
  3353. *
  3354. * The uninstall hook must be implemented in the module's .install file. It
  3355. * will fire when the module gets uninstalled but before the module's database
  3356. * tables are removed, allowing your module to query its own tables during
  3357. * this routine.
  3358. *
  3359. * When hook_uninstall() is called, your module will already be disabled, so
  3360. * its .module file will not be automatically included. If you need to call API
  3361. * functions from your .module file in this hook, use drupal_load() to make
  3362. * them available. (Keep this usage to a minimum, though, especially when
  3363. * calling API functions that invoke hooks, or API functions from modules
  3364. * listed as dependencies, since these may not be available or work as expected
  3365. * when the module is disabled.)
  3366. *
  3367. * @see hook_install()
  3368. * @see hook_schema()
  3369. * @see hook_disable()
  3370. * @see hook_modules_uninstalled()
  3371. */
  3372. function hook_uninstall() {
  3373. variable_del('upload_file_types');
  3374. }
  3375. /**
  3376. * Perform necessary actions after module is enabled.
  3377. *
  3378. * The hook is called every time the module is enabled. It should be
  3379. * implemented in the module's .install file. The implementation can
  3380. * rely on the .module file being loaded.
  3381. *
  3382. * @see module_enable()
  3383. * @see hook_install()
  3384. * @see hook_modules_enabled()
  3385. */
  3386. function hook_enable() {
  3387. mymodule_cache_rebuild();
  3388. }
  3389. /**
  3390. * Perform necessary actions before module is disabled.
  3391. *
  3392. * The hook is called every time the module is disabled. It should be
  3393. * implemented in the module's .install file. The implementation can rely
  3394. * on the .module file being loaded.
  3395. *
  3396. * @see hook_uninstall()
  3397. * @see hook_modules_disabled()
  3398. */
  3399. function hook_disable() {
  3400. mymodule_cache_rebuild();
  3401. }
  3402. /**
  3403. * Perform necessary alterations to the list of files parsed by the registry.
  3404. *
  3405. * Modules can manually modify the list of files before the registry parses
  3406. * them. The $modules array provides the .info file information, which includes
  3407. * the list of files registered to each module. Any files in the list can then
  3408. * be added to the list of files that the registry will parse, or modify
  3409. * attributes of a file.
  3410. *
  3411. * A necessary alteration made by the core SimpleTest module is to force .test
  3412. * files provided by disabled modules into the list of files parsed by the
  3413. * registry.
  3414. *
  3415. * @param $files
  3416. * List of files to be parsed by the registry. The list will contain
  3417. * files found in each enabled module's info file and the core includes
  3418. * directory. The array is keyed by the file path and contains an array of
  3419. * the related module's name and weight as used internally by
  3420. * _registry_update() and related functions.
  3421. *
  3422. * For example:
  3423. * @code
  3424. * $files["modules/system/system.module"] = array(
  3425. * 'module' => 'system',
  3426. * 'weight' => 0,
  3427. * );
  3428. * @endcode
  3429. * @param $modules
  3430. * An array containing all module information stored in the {system} table.
  3431. * Each element of the array also contains the module's .info file
  3432. * information in the property 'info'. An additional 'dir' property has been
  3433. * added to the module information which provides the path to the directory
  3434. * in which the module resides. The example shows how to take advantage of
  3435. * both properties.
  3436. *
  3437. * @see _registry_update()
  3438. * @see simpletest_test_get_all()
  3439. */
  3440. function hook_registry_files_alter(&$files, $modules) {
  3441. foreach ($modules as $module) {
  3442. // Only add test files for disabled modules, as enabled modules should
  3443. // already include any test files they provide.
  3444. if (!$module->status) {
  3445. $dir = $module->dir;
  3446. foreach ($module->info['files'] as $file) {
  3447. if (substr($file, -5) == '.test') {
  3448. $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
  3449. }
  3450. }
  3451. }
  3452. }
  3453. }
  3454. /**
  3455. * Return an array of tasks to be performed by an installation profile.
  3456. *
  3457. * Any tasks you define here will be run, in order, after the installer has
  3458. * finished the site configuration step but before it has moved on to the
  3459. * final import of languages and the end of the installation. You can have any
  3460. * number of custom tasks to perform during this phase.
  3461. *
  3462. * Each task you define here corresponds to a callback function which you must
  3463. * separately define and which is called when your task is run. This function
  3464. * will receive the global installation state variable, $install_state, as
  3465. * input, and has the opportunity to access or modify any of its settings. See
  3466. * the install_state_defaults() function in the installer for the list of
  3467. * $install_state settings used by Drupal core.
  3468. *
  3469. * At the end of your task function, you can indicate that you want the
  3470. * installer to pause and display a page to the user by returning any themed
  3471. * output that should be displayed on that page (but see below for tasks that
  3472. * use the form API or batch API; the return values of these task functions are
  3473. * handled differently). You should also use drupal_set_title() within the task
  3474. * callback function to set a custom page title. For some tasks, however, you
  3475. * may want to simply do some processing and pass control to the next task
  3476. * without ending the page request; to indicate this, simply do not send back
  3477. * a return value from your task function at all. This can be used, for
  3478. * example, by installation profiles that need to configure certain site
  3479. * settings in the database without obtaining any input from the user.
  3480. *
  3481. * The task function is treated specially if it defines a form or requires
  3482. * batch processing; in that case, you should return either the form API
  3483. * definition or batch API array, as appropriate. See below for more
  3484. * information on the 'type' key that you must define in the task definition
  3485. * to inform the installer that your task falls into one of those two
  3486. * categories. It is important to use these APIs directly, since the installer
  3487. * may be run non-interactively (for example, via a command line script), all
  3488. * in one page request; in that case, the installer will automatically take
  3489. * care of submitting forms and processing batches correctly for both types of
  3490. * installations. You can inspect the $install_state['interactive'] boolean to
  3491. * see whether or not the current installation is interactive, if you need
  3492. * access to this information.
  3493. *
  3494. * Remember that a user installing Drupal interactively will be able to reload
  3495. * an installation page multiple times, so you should use variable_set() and
  3496. * variable_get() if you are collecting any data that you need to store and
  3497. * inspect later. It is important to remove any temporary variables using
  3498. * variable_del() before your last task has completed and control is handed
  3499. * back to the installer.
  3500. *
  3501. * @param array $install_state
  3502. * An array of information about the current installation state.
  3503. *
  3504. * @return
  3505. * A keyed array of tasks the profile will perform during the final stage of
  3506. * the installation. Each key represents the name of a function (usually a
  3507. * function defined by this profile, although that is not strictly required)
  3508. * that is called when that task is run. The values are associative arrays
  3509. * containing the following key-value pairs (all of which are optional):
  3510. * - 'display_name'
  3511. * The human-readable name of the task. This will be displayed to the
  3512. * user while the installer is running, along with a list of other tasks
  3513. * that are being run. Leave this unset to prevent the task from
  3514. * appearing in the list.
  3515. * - 'display'
  3516. * This is a boolean which can be used to provide finer-grained control
  3517. * over whether or not the task will display. This is mostly useful for
  3518. * tasks that are intended to display only under certain conditions; for
  3519. * these tasks, you can set 'display_name' to the name that you want to
  3520. * display, but then use this boolean to hide the task only when certain
  3521. * conditions apply.
  3522. * - 'type'
  3523. * A string representing the type of task. This parameter has three
  3524. * possible values:
  3525. * - 'normal': This indicates that the task will be treated as a regular
  3526. * callback function, which does its processing and optionally returns
  3527. * HTML output. This is the default behavior which is used when 'type' is
  3528. * not set.
  3529. * - 'batch': This indicates that the task function will return a batch
  3530. * API definition suitable for batch_set(). The installer will then take
  3531. * care of automatically running the task via batch processing.
  3532. * - 'form': This indicates that the task function will return a standard
  3533. * form API definition (and separately define validation and submit
  3534. * handlers, as appropriate). The installer will then take care of
  3535. * automatically directing the user through the form submission process.
  3536. * - 'run'
  3537. * A constant representing the manner in which the task will be run. This
  3538. * parameter has three possible values:
  3539. * - INSTALL_TASK_RUN_IF_NOT_COMPLETED: This indicates that the task will
  3540. * run once during the installation of the profile. This is the default
  3541. * behavior which is used when 'run' is not set.
  3542. * - INSTALL_TASK_SKIP: This indicates that the task will not run during
  3543. * the current installation page request. It can be used to skip running
  3544. * an installation task when certain conditions are met, even though the
  3545. * task may still show on the list of installation tasks presented to the
  3546. * user.
  3547. * - INSTALL_TASK_RUN_IF_REACHED: This indicates that the task will run
  3548. * on each installation page request that reaches it. This is rarely
  3549. * necessary for an installation profile to use; it is primarily used by
  3550. * the Drupal installer for bootstrap-related tasks.
  3551. * - 'function'
  3552. * Normally this does not need to be set, but it can be used to force the
  3553. * installer to call a different function when the task is run (rather
  3554. * than the function whose name is given by the array key). This could be
  3555. * used, for example, to allow the same function to be called by two
  3556. * different tasks.
  3557. *
  3558. * @see install_state_defaults()
  3559. * @see batch_set()
  3560. */
  3561. function hook_install_tasks(&$install_state) {
  3562. // Here, we define a variable to allow tasks to indicate that a particular,
  3563. // processor-intensive batch process needs to be triggered later on in the
  3564. // installation.
  3565. $myprofile_needs_batch_processing = variable_get('myprofile_needs_batch_processing', FALSE);
  3566. $tasks = array(
  3567. // This is an example of a task that defines a form which the user who is
  3568. // installing the site will be asked to fill out. To implement this task,
  3569. // your profile would define a function named myprofile_data_import_form()
  3570. // as a normal form API callback function, with associated validation and
  3571. // submit handlers. In the submit handler, in addition to saving whatever
  3572. // other data you have collected from the user, you might also call
  3573. // variable_set('myprofile_needs_batch_processing', TRUE) if the user has
  3574. // entered data which requires that batch processing will need to occur
  3575. // later on.
  3576. 'myprofile_data_import_form' => array(
  3577. 'display_name' => st('Data import options'),
  3578. 'type' => 'form',
  3579. ),
  3580. // Similarly, to implement this task, your profile would define a function
  3581. // named myprofile_settings_form() with associated validation and submit
  3582. // handlers. This form might be used to collect and save additional
  3583. // information from the user that your profile needs. There are no extra
  3584. // steps required for your profile to act as an "installation wizard"; you
  3585. // can simply define as many tasks of type 'form' as you wish to execute,
  3586. // and the forms will be presented to the user, one after another.
  3587. 'myprofile_settings_form' => array(
  3588. 'display_name' => st('Additional options'),
  3589. 'type' => 'form',
  3590. ),
  3591. // This is an example of a task that performs batch operations. To
  3592. // implement this task, your profile would define a function named
  3593. // myprofile_batch_processing() which returns a batch API array definition
  3594. // that the installer will use to execute your batch operations. Due to the
  3595. // 'myprofile_needs_batch_processing' variable used here, this task will be
  3596. // hidden and skipped unless your profile set it to TRUE in one of the
  3597. // previous tasks.
  3598. 'myprofile_batch_processing' => array(
  3599. 'display_name' => st('Import additional data'),
  3600. 'display' => $myprofile_needs_batch_processing,
  3601. 'type' => 'batch',
  3602. 'run' => $myprofile_needs_batch_processing ? INSTALL_TASK_RUN_IF_NOT_COMPLETED : INSTALL_TASK_SKIP,
  3603. ),
  3604. // This is an example of a task that will not be displayed in the list that
  3605. // the user sees. To implement this task, your profile would define a
  3606. // function named myprofile_final_site_setup(), in which additional,
  3607. // automated site setup operations would be performed. Since this is the
  3608. // last task defined by your profile, you should also use this function to
  3609. // call variable_del('myprofile_needs_batch_processing') and clean up the
  3610. // variable that was used above. If you want the user to pass to the final
  3611. // Drupal installation tasks uninterrupted, return no output from this
  3612. // function. Otherwise, return themed output that the user will see (for
  3613. // example, a confirmation page explaining that your profile's tasks are
  3614. // complete, with a link to reload the current page and therefore pass on
  3615. // to the final Drupal installation tasks when the user is ready to do so).
  3616. 'myprofile_final_site_setup' => array(
  3617. ),
  3618. );
  3619. return $tasks;
  3620. }
  3621. /**
  3622. * Change the page the user is sent to by drupal_goto().
  3623. *
  3624. * @param $path
  3625. * A Drupal path or a full URL.
  3626. * @param $options
  3627. * An associative array of additional URL options to pass to url().
  3628. * @param $http_response_code
  3629. * The HTTP status code to use for the redirection. See drupal_goto() for more
  3630. * information.
  3631. */
  3632. function hook_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  3633. // A good addition to misery module.
  3634. $http_response_code = 500;
  3635. }
  3636. /**
  3637. * Alter XHTML HEAD tags before they are rendered by drupal_get_html_head().
  3638. *
  3639. * Elements available to be altered are only those added using
  3640. * drupal_add_html_head_link() or drupal_add_html_head(). CSS and JS files
  3641. * are handled using drupal_add_css() and drupal_add_js(), so the head links
  3642. * for those files will not appear in the $head_elements array.
  3643. *
  3644. * @param $head_elements
  3645. * An array of renderable elements. Generally the values of the #attributes
  3646. * array will be the most likely target for changes.
  3647. */
  3648. function hook_html_head_alter(&$head_elements) {
  3649. foreach ($head_elements as $key => $element) {
  3650. if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
  3651. // I want a custom canonical URL.
  3652. $head_elements[$key]['#attributes']['href'] = mymodule_canonical_url();
  3653. }
  3654. }
  3655. }
  3656. /**
  3657. * Alter the full list of installation tasks.
  3658. *
  3659. * @param $tasks
  3660. * An array of all available installation tasks, including those provided by
  3661. * Drupal core. You can modify this array to change or replace any part of
  3662. * the Drupal installation process that occurs after the installation profile
  3663. * is selected.
  3664. * @param $install_state
  3665. * An array of information about the current installation state.
  3666. */
  3667. function hook_install_tasks_alter(&$tasks, $install_state) {
  3668. // Replace the "Choose language" installation task provided by Drupal core
  3669. // with a custom callback function defined by this installation profile.
  3670. $tasks['install_select_locale']['function'] = 'myprofile_locale_selection';
  3671. }
  3672. /**
  3673. * Alter MIME type mappings used to determine MIME type from a file extension.
  3674. *
  3675. * This hook is run when file_mimetype_mapping() is called. It is used to
  3676. * allow modules to add to or modify the default mapping from
  3677. * file_default_mimetype_mapping().
  3678. *
  3679. * @param $mapping
  3680. * An array of mimetypes correlated to the extensions that relate to them.
  3681. * The array has 'mimetypes' and 'extensions' elements, each of which is an
  3682. * array.
  3683. *
  3684. * @see file_default_mimetype_mapping()
  3685. */
  3686. function hook_file_mimetype_mapping_alter(&$mapping) {
  3687. // Add new MIME type 'drupal/info'.
  3688. $mapping['mimetypes']['example_info'] = 'drupal/info';
  3689. // Add new extension '.info' and map it to the 'drupal/info' MIME type.
  3690. $mapping['extensions']['info'] = 'example_info';
  3691. // Override existing extension mapping for '.ogg' files.
  3692. $mapping['extensions']['ogg'] = 189;
  3693. }
  3694. /**
  3695. * Declares information about actions.
  3696. *
  3697. * Any module can define actions, and then call actions_do() to make those
  3698. * actions happen in response to events. The trigger module provides a user
  3699. * interface for associating actions with module-defined triggers, and it makes
  3700. * sure the core triggers fire off actions when their events happen.
  3701. *
  3702. * An action consists of two or three parts:
  3703. * - an action definition (returned by this hook)
  3704. * - a function which performs the action (which by convention is named
  3705. * MODULE_description-of-function_action)
  3706. * - an optional form definition function that defines a configuration form
  3707. * (which has the name of the action function with '_form' appended to it.)
  3708. *
  3709. * The action function takes two to four arguments, which come from the input
  3710. * arguments to actions_do().
  3711. *
  3712. * @return
  3713. * An associative array of action descriptions. The keys of the array
  3714. * are the names of the action functions, and each corresponding value
  3715. * is an associative array with the following key-value pairs:
  3716. * - 'type': The type of object this action acts upon. Core actions have types
  3717. * 'node', 'user', 'comment', and 'system'.
  3718. * - 'label': The human-readable name of the action, which should be passed
  3719. * through the t() function for translation.
  3720. * - 'configurable': If FALSE, then the action doesn't require any extra
  3721. * configuration. If TRUE, then your module must define a form function with
  3722. * the same name as the action function with '_form' appended (e.g., the
  3723. * form for 'node_assign_owner_action' is 'node_assign_owner_action_form'.)
  3724. * This function takes $context as its only parameter, and is paired with
  3725. * the usual _submit function, and possibly a _validate function.
  3726. * - 'triggers': An array of the events (that is, hooks) that can trigger this
  3727. * action. For example: array('node_insert', 'user_update'). You can also
  3728. * declare support for any trigger by returning array('any') for this value.
  3729. * - 'behavior': (optional) A machine-readable array of behaviors of this
  3730. * action, used to signal additionally required actions that may need to be
  3731. * triggered. Currently recognized behaviors by Trigger module:
  3732. * - 'changes_property': If an action with this behavior is assigned to a
  3733. * trigger other than a "presave" hook, any save actions also assigned to
  3734. * this trigger are moved later in the list. If no save action is present,
  3735. * one will be added.
  3736. * Modules that are processing actions (like Trigger module) should take
  3737. * special care for the "presave" hook, in which case a dependent "save"
  3738. * action should NOT be invoked.
  3739. *
  3740. * @ingroup actions
  3741. */
  3742. function hook_action_info() {
  3743. return array(
  3744. 'comment_unpublish_action' => array(
  3745. 'type' => 'comment',
  3746. 'label' => t('Unpublish comment'),
  3747. 'configurable' => FALSE,
  3748. 'behavior' => array('changes_property'),
  3749. 'triggers' => array('comment_presave', 'comment_insert', 'comment_update'),
  3750. ),
  3751. 'comment_unpublish_by_keyword_action' => array(
  3752. 'type' => 'comment',
  3753. 'label' => t('Unpublish comment containing keyword(s)'),
  3754. 'configurable' => TRUE,
  3755. 'behavior' => array('changes_property'),
  3756. 'triggers' => array('comment_presave', 'comment_insert', 'comment_update'),
  3757. ),
  3758. 'comment_save_action' => array(
  3759. 'type' => 'comment',
  3760. 'label' => t('Save comment'),
  3761. 'configurable' => FALSE,
  3762. 'triggers' => array('comment_insert', 'comment_update'),
  3763. ),
  3764. );
  3765. }
  3766. /**
  3767. * Executes code after an action is deleted.
  3768. *
  3769. * @param $aid
  3770. * The action ID.
  3771. */
  3772. function hook_actions_delete($aid) {
  3773. db_delete('actions_assignments')
  3774. ->condition('aid', $aid)
  3775. ->execute();
  3776. }
  3777. /**
  3778. * Alters the actions declared by another module.
  3779. *
  3780. * Called by actions_list() to allow modules to alter the return values from
  3781. * implementations of hook_action_info().
  3782. *
  3783. * @see trigger_example_action_info_alter()
  3784. */
  3785. function hook_action_info_alter(&$actions) {
  3786. $actions['node_unpublish_action']['label'] = t('Unpublish and remove from public view.');
  3787. }
  3788. /**
  3789. * Declare archivers to the system.
  3790. *
  3791. * An archiver is a class that is able to package and unpackage one or more files
  3792. * into a single possibly compressed file. Common examples of such files are
  3793. * zip files and tar.gz files. All archiver classes must implement
  3794. * ArchiverInterface.
  3795. *
  3796. * Each entry should be keyed on a unique value, and specify three
  3797. * additional keys:
  3798. * - class: The name of the PHP class for this archiver.
  3799. * - extensions: An array of file extensions that this archiver supports.
  3800. * - weight: This optional key specifies the weight of this archiver.
  3801. * When mapping file extensions to archivers, the first archiver by
  3802. * weight found that supports the requested extension will be used.
  3803. *
  3804. * @see hook_archiver_info_alter()
  3805. */
  3806. function hook_archiver_info() {
  3807. return array(
  3808. 'tar' => array(
  3809. 'class' => 'ArchiverTar',
  3810. 'extensions' => array('tar', 'tar.gz', 'tar.bz2'),
  3811. ),
  3812. );
  3813. }
  3814. /**
  3815. * Alter archiver information declared by other modules.
  3816. *
  3817. * See hook_archiver_info() for a description of archivers and the archiver
  3818. * information structure.
  3819. *
  3820. * @param $info
  3821. * Archiver information to alter (return values from hook_archiver_info()).
  3822. */
  3823. function hook_archiver_info_alter(&$info) {
  3824. $info['tar']['extensions'][] = 'tgz';
  3825. }
  3826. /**
  3827. * Define additional date types.
  3828. *
  3829. * Next to the 'long', 'medium' and 'short' date types defined in core, any
  3830. * module can define additional types that can be used when displaying dates,
  3831. * by implementing this hook. A date type is basically just a name for a date
  3832. * format.
  3833. *
  3834. * Date types are used in the administration interface: a user can assign
  3835. * date format types defined in hook_date_formats() to date types defined in
  3836. * this hook. Once a format has been assigned by a user, the machine name of a
  3837. * type can be used in the format_date() function to format a date using the
  3838. * chosen formatting.
  3839. *
  3840. * To define a date type in a module and make sure a format has been assigned to
  3841. * it, without requiring a user to visit the administrative interface, use
  3842. * @code variable_set('date_format_' . $type, $format); @endcode
  3843. * where $type is the machine-readable name defined here, and $format is a PHP
  3844. * date format string.
  3845. *
  3846. * To avoid namespace collisions with date types defined by other modules, it is
  3847. * recommended that each date type starts with the module name. A date type
  3848. * can consist of letters, numbers and underscores.
  3849. *
  3850. * @return
  3851. * An array of date types where the keys are the machine-readable names and
  3852. * the values are the human-readable labels.
  3853. *
  3854. * @see hook_date_formats()
  3855. * @see format_date()
  3856. */
  3857. function hook_date_format_types() {
  3858. // Define the core date format types.
  3859. return array(
  3860. 'long' => t('Long'),
  3861. 'medium' => t('Medium'),
  3862. 'short' => t('Short'),
  3863. );
  3864. }
  3865. /**
  3866. * Modify existing date types.
  3867. *
  3868. * Allows other modules to modify existing date types like 'long'. Called by
  3869. * _system_date_format_types_build(). For instance, A module may use this hook
  3870. * to apply settings across all date types, such as locking all date types so
  3871. * they appear to be provided by the system.
  3872. *
  3873. * @param $types
  3874. * A list of date types. Each date type is keyed by the machine-readable name
  3875. * and the values are associative arrays containing:
  3876. * - is_new: Set to FALSE to override previous settings.
  3877. * - module: The name of the module that created the date type.
  3878. * - type: The machine-readable date type name.
  3879. * - title: The human-readable date type name.
  3880. * - locked: Specifies that the date type is system-provided.
  3881. */
  3882. function hook_date_format_types_alter(&$types) {
  3883. foreach ($types as $name => $type) {
  3884. $types[$name]['locked'] = 1;
  3885. }
  3886. }
  3887. /**
  3888. * Define additional date formats.
  3889. *
  3890. * This hook is used to define the PHP date format strings that can be assigned
  3891. * to date types in the administrative interface. A module can provide date
  3892. * format strings for the core-provided date types ('long', 'medium', and
  3893. * 'short'), or for date types defined in hook_date_format_types() by itself
  3894. * or another module.
  3895. *
  3896. * Since date formats can be locale-specific, you can specify the locales that
  3897. * each date format string applies to. There may be more than one locale for a
  3898. * format. There may also be more than one format for the same locale. For
  3899. * example d/m/Y and Y/m/d work equally well in some locales. You may wish to
  3900. * define some additional date formats that aren't specific to any one locale,
  3901. * for example, "Y m". For these cases, the 'locales' component of the return
  3902. * value should be omitted.
  3903. *
  3904. * Providing a date format here does not normally assign the format to be
  3905. * used with the associated date type -- a user has to choose a format for each
  3906. * date type in the administrative interface. There is one exception: locale
  3907. * initialization chooses a locale-specific format for the three core-provided
  3908. * types (see locale_get_localized_date_format() for details). If your module
  3909. * needs to ensure that a date type it defines has a format associated with it,
  3910. * call @code variable_set('date_format_' . $type, $format); @endcode
  3911. * where $type is the machine-readable name defined in hook_date_format_types(),
  3912. * and $format is a PHP date format string.
  3913. *
  3914. * @return
  3915. * A list of date formats to offer as choices in the administrative
  3916. * interface. Each date format is a keyed array consisting of three elements:
  3917. * - 'type': The date type name that this format can be used with, as
  3918. * declared in an implementation of hook_date_format_types().
  3919. * - 'format': A PHP date format string to use when formatting dates. It
  3920. * can contain any of the formatting options described at
  3921. * http://php.net/manual/en/function.date.php
  3922. * - 'locales': (optional) An array of 2 and 5 character locale codes,
  3923. * defining which locales this format applies to (for example, 'en',
  3924. * 'en-us', etc.). If your date format is not language-specific, leave this
  3925. * array empty.
  3926. *
  3927. * @see hook_date_format_types()
  3928. */
  3929. function hook_date_formats() {
  3930. return array(
  3931. array(
  3932. 'type' => 'mymodule_extra_long',
  3933. 'format' => 'l jS F Y H:i:s e',
  3934. 'locales' => array('en-ie'),
  3935. ),
  3936. array(
  3937. 'type' => 'mymodule_extra_long',
  3938. 'format' => 'l jS F Y h:i:sa',
  3939. 'locales' => array('en', 'en-us'),
  3940. ),
  3941. array(
  3942. 'type' => 'short',
  3943. 'format' => 'F Y',
  3944. 'locales' => array(),
  3945. ),
  3946. );
  3947. }
  3948. /**
  3949. * Alter date formats declared by another module.
  3950. *
  3951. * Called by _system_date_format_types_build() to allow modules to alter the
  3952. * return values from implementations of hook_date_formats().
  3953. */
  3954. function hook_date_formats_alter(&$formats) {
  3955. foreach ($formats as $id => $format) {
  3956. $formats[$id]['locales'][] = 'en-ca';
  3957. }
  3958. }
  3959. /**
  3960. * Alters the delivery callback used to send the result of the page callback to the browser.
  3961. *
  3962. * Called by drupal_deliver_page() to allow modules to alter how the
  3963. * page is delivered to the browser.
  3964. *
  3965. * This hook is intended for altering the delivery callback based on
  3966. * information unrelated to the path of the page accessed. For example,
  3967. * it can be used to set the delivery callback based on a HTTP request
  3968. * header (as shown in the code sample). To specify a delivery callback
  3969. * based on path information, use hook_menu() or hook_menu_alter().
  3970. *
  3971. * This hook can also be used as an API function that can be used to explicitly
  3972. * set the delivery callback from some other function. For example, for a module
  3973. * named MODULE:
  3974. * @code
  3975. * function MODULE_page_delivery_callback_alter(&$callback, $set = FALSE) {
  3976. * static $stored_callback;
  3977. * if ($set) {
  3978. * $stored_callback = $callback;
  3979. * }
  3980. * elseif (isset($stored_callback)) {
  3981. * $callback = $stored_callback;
  3982. * }
  3983. * }
  3984. * function SOMEWHERE_ELSE() {
  3985. * $desired_delivery_callback = 'foo';
  3986. * MODULE_page_delivery_callback_alter($desired_delivery_callback, TRUE);
  3987. * }
  3988. * @endcode
  3989. *
  3990. * @param $callback
  3991. * The name of a function.
  3992. *
  3993. * @see drupal_deliver_page()
  3994. */
  3995. function hook_page_delivery_callback_alter(&$callback) {
  3996. // jQuery sets a HTTP_X_REQUESTED_WITH header of 'XMLHttpRequest'.
  3997. // If a page would normally be delivered as an html page, and it is called
  3998. // from jQuery, deliver it instead as an Ajax response.
  3999. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && $callback == 'drupal_deliver_html_page') {
  4000. $callback = 'ajax_deliver';
  4001. }
  4002. }
  4003. /**
  4004. * Alters theme operation links.
  4005. *
  4006. * @param $theme_groups
  4007. * An associative array containing groups of themes.
  4008. *
  4009. * @see system_themes_page()
  4010. */
  4011. function hook_system_themes_page_alter(&$theme_groups) {
  4012. foreach ($theme_groups as $state => &$group) {
  4013. foreach ($theme_groups[$state] as &$theme) {
  4014. // Add a foo link to each list of theme operations.
  4015. $theme->operations[] = array(
  4016. 'title' => t('Foo'),
  4017. 'href' => 'admin/appearance/foo',
  4018. 'query' => array('theme' => $theme->name)
  4019. );
  4020. }
  4021. }
  4022. }
  4023. /**
  4024. * Alters inbound URL requests.
  4025. *
  4026. * @param $path
  4027. * The path being constructed, which, if a path alias, has been resolved to a
  4028. * Drupal path by the database, and which also may have been altered by other
  4029. * modules before this one.
  4030. * @param $original_path
  4031. * The original path, before being checked for path aliases or altered by any
  4032. * modules.
  4033. * @param $path_language
  4034. * The language of the path.
  4035. *
  4036. * @see drupal_get_normal_path()
  4037. */
  4038. function hook_url_inbound_alter(&$path, $original_path, $path_language) {
  4039. // Create the path user/me/edit, which allows a user to edit their account.
  4040. if (preg_match('|^user/me/edit(/.*)?|', $path, $matches)) {
  4041. global $user;
  4042. $path = 'user/' . $user->uid . '/edit' . $matches[1];
  4043. }
  4044. }
  4045. /**
  4046. * Alters outbound URLs.
  4047. *
  4048. * @param $path
  4049. * The outbound path to alter, not adjusted for path aliases yet. It won't be
  4050. * adjusted for path aliases until all modules are finished altering it, thus
  4051. * being consistent with hook_url_inbound_alter(), which adjusts for all path
  4052. * aliases before allowing modules to alter it. This may have been altered by
  4053. * other modules before this one.
  4054. * @param $options
  4055. * A set of URL options for the URL so elements such as a fragment or a query
  4056. * string can be added to the URL.
  4057. * @param $original_path
  4058. * The original path, before being altered by any modules.
  4059. *
  4060. * @see url()
  4061. */
  4062. function hook_url_outbound_alter(&$path, &$options, $original_path) {
  4063. // Use an external RSS feed rather than the Drupal one.
  4064. if ($path == 'rss.xml') {
  4065. $path = 'http://example.com/rss.xml';
  4066. $options['external'] = TRUE;
  4067. }
  4068. // Instead of pointing to user/[uid]/edit, point to user/me/edit.
  4069. if (preg_match('|^user/([0-9]*)/edit(/.*)?|', $path, $matches)) {
  4070. global $user;
  4071. if ($user->uid == $matches[1]) {
  4072. $path = 'user/me/edit' . $matches[2];
  4073. }
  4074. }
  4075. }
  4076. /**
  4077. * Alter the username that is displayed for a user.
  4078. *
  4079. * Called by format_username() to allow modules to alter the username that's
  4080. * displayed. Can be used to ensure user privacy in situations where
  4081. * $account->name is too revealing.
  4082. *
  4083. * @param $name
  4084. * The string that format_username() will return.
  4085. *
  4086. * @param $account
  4087. * The account object passed to format_username().
  4088. *
  4089. * @see format_username()
  4090. */
  4091. function hook_username_alter(&$name, $account) {
  4092. // Display the user's uid instead of name.
  4093. if (isset($account->uid)) {
  4094. $name = t('User !uid', array('!uid' => $account->uid));
  4095. }
  4096. }
  4097. /**
  4098. * Provide replacement values for placeholder tokens.
  4099. *
  4100. * This hook is invoked when someone calls token_replace(). That function first
  4101. * scans the text for [type:token] patterns, and splits the needed tokens into
  4102. * groups by type. Then hook_tokens() is invoked on each token-type group,
  4103. * allowing your module to respond by providing replacement text for any of
  4104. * the tokens in the group that your module knows how to process.
  4105. *
  4106. * A module implementing this hook should also implement hook_token_info() in
  4107. * order to list its available tokens on editing screens.
  4108. *
  4109. * @param $type
  4110. * The machine-readable name of the type (group) of token being replaced, such
  4111. * as 'node', 'user', or another type defined by a hook_token_info()
  4112. * implementation.
  4113. * @param $tokens
  4114. * An array of tokens to be replaced. The keys are the machine-readable token
  4115. * names, and the values are the raw [type:token] strings that appeared in the
  4116. * original text.
  4117. * @param $data
  4118. * (optional) An associative array of data objects to be used when generating
  4119. * replacement values, as supplied in the $data parameter to token_replace().
  4120. * @param $options
  4121. * (optional) An associative array of options for token replacement; see
  4122. * token_replace() for possible values.
  4123. *
  4124. * @return
  4125. * An associative array of replacement values, keyed by the raw [type:token]
  4126. * strings from the original text.
  4127. *
  4128. * @see hook_token_info()
  4129. * @see hook_tokens_alter()
  4130. */
  4131. function hook_tokens($type, $tokens, array $data = array(), array $options = array()) {
  4132. $url_options = array('absolute' => TRUE);
  4133. if (isset($options['language'])) {
  4134. $url_options['language'] = $options['language'];
  4135. $language_code = $options['language']->language;
  4136. }
  4137. else {
  4138. $language_code = NULL;
  4139. }
  4140. $sanitize = !empty($options['sanitize']);
  4141. $replacements = array();
  4142. if ($type == 'node' && !empty($data['node'])) {
  4143. $node = $data['node'];
  4144. foreach ($tokens as $name => $original) {
  4145. switch ($name) {
  4146. // Simple key values on the node.
  4147. case 'nid':
  4148. $replacements[$original] = $node->nid;
  4149. break;
  4150. case 'title':
  4151. $replacements[$original] = $sanitize ? check_plain($node->title) : $node->title;
  4152. break;
  4153. case 'edit-url':
  4154. $replacements[$original] = url('node/' . $node->nid . '/edit', $url_options);
  4155. break;
  4156. // Default values for the chained tokens handled below.
  4157. case 'author':
  4158. $name = ($node->uid == 0) ? variable_get('anonymous', t('Anonymous')) : $node->name;
  4159. $replacements[$original] = $sanitize ? filter_xss($name) : $name;
  4160. break;
  4161. case 'created':
  4162. $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
  4163. break;
  4164. }
  4165. }
  4166. if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
  4167. $author = user_load($node->uid);
  4168. $replacements += token_generate('user', $author_tokens, array('user' => $author), $options);
  4169. }
  4170. if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
  4171. $replacements += token_generate('date', $created_tokens, array('date' => $node->created), $options);
  4172. }
  4173. }
  4174. return $replacements;
  4175. }
  4176. /**
  4177. * Alter replacement values for placeholder tokens.
  4178. *
  4179. * @param $replacements
  4180. * An associative array of replacements returned by hook_tokens().
  4181. * @param $context
  4182. * The context in which hook_tokens() was called. An associative array with
  4183. * the following keys, which have the same meaning as the corresponding
  4184. * parameters of hook_tokens():
  4185. * - 'type'
  4186. * - 'tokens'
  4187. * - 'data'
  4188. * - 'options'
  4189. *
  4190. * @see hook_tokens()
  4191. */
  4192. function hook_tokens_alter(array &$replacements, array $context) {
  4193. $options = $context['options'];
  4194. if (isset($options['language'])) {
  4195. $url_options['language'] = $options['language'];
  4196. $language_code = $options['language']->language;
  4197. }
  4198. else {
  4199. $language_code = NULL;
  4200. }
  4201. $sanitize = !empty($options['sanitize']);
  4202. if ($context['type'] == 'node' && !empty($context['data']['node'])) {
  4203. $node = $context['data']['node'];
  4204. // Alter the [node:title] token, and replace it with the rendered content
  4205. // of a field (field_title).
  4206. if (isset($context['tokens']['title'])) {
  4207. $title = field_view_field('node', $node, 'field_title', 'default', $language_code);
  4208. $replacements[$context['tokens']['title']] = drupal_render($title);
  4209. }
  4210. }
  4211. }
  4212. /**
  4213. * Provide information about available placeholder tokens and token types.
  4214. *
  4215. * Tokens are placeholders that can be put into text by using the syntax
  4216. * [type:token], where type is the machine-readable name of a token type, and
  4217. * token is the machine-readable name of a token within this group. This hook
  4218. * provides a list of types and tokens to be displayed on text editing screens,
  4219. * so that people editing text can see what their token options are.
  4220. *
  4221. * The actual token replacement is done by token_replace(), which invokes
  4222. * hook_tokens(). Your module will need to implement that hook in order to
  4223. * generate token replacements from the tokens defined here.
  4224. *
  4225. * @return
  4226. * An associative array of available tokens and token types. The outer array
  4227. * has two components:
  4228. * - types: An associative array of token types (groups). Each token type is
  4229. * an associative array with the following components:
  4230. * - name: The translated human-readable short name of the token type.
  4231. * - description: A translated longer description of the token type.
  4232. * - needs-data: The type of data that must be provided to token_replace()
  4233. * in the $data argument (i.e., the key name in $data) in order for tokens
  4234. * of this type to be used in the $text being processed. For instance, if
  4235. * the token needs a node object, 'needs-data' should be 'node', and to
  4236. * use this token in token_replace(), the caller needs to supply a node
  4237. * object as $data['node']. Some token data can also be supplied
  4238. * indirectly; for instance, a node object in $data supplies a user object
  4239. * (the author of the node), allowing user tokens to be used when only
  4240. * a node data object is supplied.
  4241. * - tokens: An associative array of tokens. The outer array is keyed by the
  4242. * group name (the same key as in the types array). Within each group of
  4243. * tokens, each token item is keyed by the machine name of the token, and
  4244. * each token item has the following components:
  4245. * - name: The translated human-readable short name of the token.
  4246. * - description: A translated longer description of the token.
  4247. * - type (optional): A 'needs-data' data type supplied by this token, which
  4248. * should match a 'needs-data' value from another token type. For example,
  4249. * the node author token provides a user object, which can then be used
  4250. * for token replacement data in token_replace() without having to supply
  4251. * a separate user object.
  4252. *
  4253. * @see hook_token_info_alter()
  4254. * @see hook_tokens()
  4255. */
  4256. function hook_token_info() {
  4257. $type = array(
  4258. 'name' => t('Nodes'),
  4259. 'description' => t('Tokens related to individual nodes.'),
  4260. 'needs-data' => 'node',
  4261. );
  4262. // Core tokens for nodes.
  4263. $node['nid'] = array(
  4264. 'name' => t("Node ID"),
  4265. 'description' => t("The unique ID of the node."),
  4266. );
  4267. $node['title'] = array(
  4268. 'name' => t("Title"),
  4269. 'description' => t("The title of the node."),
  4270. );
  4271. $node['edit-url'] = array(
  4272. 'name' => t("Edit URL"),
  4273. 'description' => t("The URL of the node's edit page."),
  4274. );
  4275. // Chained tokens for nodes.
  4276. $node['created'] = array(
  4277. 'name' => t("Date created"),
  4278. 'description' => t("The date the node was posted."),
  4279. 'type' => 'date',
  4280. );
  4281. $node['author'] = array(
  4282. 'name' => t("Author"),
  4283. 'description' => t("The author of the node."),
  4284. 'type' => 'user',
  4285. );
  4286. return array(
  4287. 'types' => array('node' => $type),
  4288. 'tokens' => array('node' => $node),
  4289. );
  4290. }
  4291. /**
  4292. * Alter the metadata about available placeholder tokens and token types.
  4293. *
  4294. * @param $data
  4295. * The associative array of token definitions from hook_token_info().
  4296. *
  4297. * @see hook_token_info()
  4298. */
  4299. function hook_token_info_alter(&$data) {
  4300. // Modify description of node tokens for our site.
  4301. $data['tokens']['node']['nid'] = array(
  4302. 'name' => t("Node ID"),
  4303. 'description' => t("The unique ID of the article."),
  4304. );
  4305. $data['tokens']['node']['title'] = array(
  4306. 'name' => t("Title"),
  4307. 'description' => t("The title of the article."),
  4308. );
  4309. // Chained tokens for nodes.
  4310. $data['tokens']['node']['created'] = array(
  4311. 'name' => t("Date created"),
  4312. 'description' => t("The date the article was posted."),
  4313. 'type' => 'date',
  4314. );
  4315. }
  4316. /**
  4317. * Alter batch information before a batch is processed.
  4318. *
  4319. * Called by batch_process() to allow modules to alter a batch before it is
  4320. * processed.
  4321. *
  4322. * @param $batch
  4323. * The associative array of batch information. See batch_set() for details on
  4324. * what this could contain.
  4325. *
  4326. * @see batch_set()
  4327. * @see batch_process()
  4328. *
  4329. * @ingroup batch
  4330. */
  4331. function hook_batch_alter(&$batch) {
  4332. // If the current page request is inside the overlay, add ?render=overlay to
  4333. // the success callback URL, so that it appears correctly within the overlay.
  4334. if (overlay_get_mode() == 'child') {
  4335. if (isset($batch['url_options']['query'])) {
  4336. $batch['url_options']['query']['render'] = 'overlay';
  4337. }
  4338. else {
  4339. $batch['url_options']['query'] = array('render' => 'overlay');
  4340. }
  4341. }
  4342. }
  4343. /**
  4344. * Provide information on Updaters (classes that can update Drupal).
  4345. *
  4346. * An Updater is a class that knows how to update various parts of the Drupal
  4347. * file system, for example to update modules that have newer releases, or to
  4348. * install a new theme.
  4349. *
  4350. * @return
  4351. * An associative array of information about the updater(s) being provided.
  4352. * This array is keyed by a unique identifier for each updater, and the
  4353. * values are subarrays that can contain the following keys:
  4354. * - class: The name of the PHP class which implements this updater.
  4355. * - name: Human-readable name of this updater.
  4356. * - weight: Controls what order the Updater classes are consulted to decide
  4357. * which one should handle a given task. When an update task is being run,
  4358. * the system will loop through all the Updater classes defined in this
  4359. * registry in weight order and let each class respond to the task and
  4360. * decide if each Updater wants to handle the task. In general, this
  4361. * doesn't matter, but if you need to override an existing Updater, make
  4362. * sure your Updater has a lighter weight so that it comes first.
  4363. *
  4364. * @see drupal_get_updaters()
  4365. * @see hook_updater_info_alter()
  4366. */
  4367. function hook_updater_info() {
  4368. return array(
  4369. 'module' => array(
  4370. 'class' => 'ModuleUpdater',
  4371. 'name' => t('Update modules'),
  4372. 'weight' => 0,
  4373. ),
  4374. 'theme' => array(
  4375. 'class' => 'ThemeUpdater',
  4376. 'name' => t('Update themes'),
  4377. 'weight' => 0,
  4378. ),
  4379. );
  4380. }
  4381. /**
  4382. * Alter the Updater information array.
  4383. *
  4384. * An Updater is a class that knows how to update various parts of the Drupal
  4385. * file system, for example to update modules that have newer releases, or to
  4386. * install a new theme.
  4387. *
  4388. * @param array $updaters
  4389. * Associative array of updaters as defined through hook_updater_info().
  4390. * Alter this array directly.
  4391. *
  4392. * @see drupal_get_updaters()
  4393. * @see hook_updater_info()
  4394. */
  4395. function hook_updater_info_alter(&$updaters) {
  4396. // Adjust weight so that the theme Updater gets a chance to handle a given
  4397. // update task before module updaters.
  4398. $updaters['theme']['weight'] = -1;
  4399. }
  4400. /**
  4401. * Alter the default country list.
  4402. *
  4403. * @param $countries
  4404. * The associative array of countries keyed by ISO 3166-1 country code.
  4405. *
  4406. * @see country_get_list()
  4407. * @see _country_get_predefined_list()
  4408. */
  4409. function hook_countries_alter(&$countries) {
  4410. // Elbonia is now independent, so add it to the country list.
  4411. $countries['EB'] = 'Elbonia';
  4412. }
  4413. /**
  4414. * Control site status before menu dispatching.
  4415. *
  4416. * The hook is called after checking whether the site is offline but before
  4417. * the current router item is retrieved and executed by
  4418. * menu_execute_active_handler(). If the site is in offline mode,
  4419. * $menu_site_status is set to MENU_SITE_OFFLINE.
  4420. *
  4421. * @param $menu_site_status
  4422. * Supported values are MENU_SITE_OFFLINE, MENU_ACCESS_DENIED,
  4423. * MENU_NOT_FOUND and MENU_SITE_ONLINE. Any other value than
  4424. * MENU_SITE_ONLINE will skip the default menu handling system and be passed
  4425. * for delivery to drupal_deliver_page() with a NULL
  4426. * $default_delivery_callback.
  4427. * @param $path
  4428. * Contains the system path that is going to be loaded. This is read only,
  4429. * use hook_url_inbound_alter() to change the path.
  4430. */
  4431. function hook_menu_site_status_alter(&$menu_site_status, $path) {
  4432. // Allow access to my_module/authentication even if site is in offline mode.
  4433. if ($menu_site_status == MENU_SITE_OFFLINE && user_is_anonymous() && $path == 'my_module/authentication') {
  4434. $menu_site_status = MENU_SITE_ONLINE;
  4435. }
  4436. }
  4437. /**
  4438. * Register information about FileTransfer classes provided by a module.
  4439. *
  4440. * The FileTransfer class allows transferring files over a specific type of
  4441. * connection. Core provides classes for FTP and SSH. Contributed modules are
  4442. * free to extend the FileTransfer base class to add other connection types,
  4443. * and if these classes are registered via hook_filetransfer_info(), those
  4444. * connection types will be available to site administrators using the Update
  4445. * manager when they are redirected to the authorize.php script to authorize
  4446. * the file operations.
  4447. *
  4448. * @return array
  4449. * Nested array of information about FileTransfer classes. Each key is a
  4450. * FileTransfer type (not human readable, used for form elements and
  4451. * variable names, etc), and the values are subarrays that define properties
  4452. * of that type. The keys in each subarray are:
  4453. * - 'title': Required. The human-readable name of the connection type.
  4454. * - 'class': Required. The name of the FileTransfer class. The constructor
  4455. * will always be passed the full path to the root of the site that should
  4456. * be used to restrict where file transfer operations can occur (the $jail)
  4457. * and an array of settings values returned by the settings form.
  4458. * - 'file': Required. The include file containing the FileTransfer class.
  4459. * This should be a separate .inc file, not just the .module file, so that
  4460. * the minimum possible code is loaded when authorize.php is running.
  4461. * - 'file path': Optional. The directory (relative to the Drupal root)
  4462. * where the include file lives. If not defined, defaults to the base
  4463. * directory of the module implementing the hook.
  4464. * - 'weight': Optional. Integer weight used for sorting connection types on
  4465. * the authorize.php form.
  4466. *
  4467. * @see FileTransfer
  4468. * @see authorize.php
  4469. * @see hook_filetransfer_info_alter()
  4470. * @see drupal_get_filetransfer_info()
  4471. */
  4472. function hook_filetransfer_info() {
  4473. $info['sftp'] = array(
  4474. 'title' => t('SFTP (Secure FTP)'),
  4475. 'file' => 'sftp.filetransfer.inc',
  4476. 'class' => 'FileTransferSFTP',
  4477. 'weight' => 10,
  4478. );
  4479. return $info;
  4480. }
  4481. /**
  4482. * Alter the FileTransfer class registry.
  4483. *
  4484. * @param array $filetransfer_info
  4485. * Reference to a nested array containing information about the FileTransfer
  4486. * class registry.
  4487. *
  4488. * @see hook_filetransfer_info()
  4489. */
  4490. function hook_filetransfer_info_alter(&$filetransfer_info) {
  4491. if (variable_get('paranoia', FALSE)) {
  4492. // Remove the FTP option entirely.
  4493. unset($filetransfer_info['ftp']);
  4494. // Make sure the SSH option is listed first.
  4495. $filetransfer_info['ssh']['weight'] = -10;
  4496. }
  4497. }
  4498. /**
  4499. * @} End of "addtogroup hooks".
  4500. */
  4501. /**
  4502. * @defgroup update_api Update versions of API functions
  4503. * @{
  4504. * Functions that are similar to normal API functions, but do not invoke hooks.
  4505. *
  4506. * These simplified versions of core API functions are provided for use by
  4507. * update functions (hook_update_N() implementations).
  4508. *
  4509. * During database updates the schema of any module could be out of date. For
  4510. * this reason, caution is needed when using any API function within an update
  4511. * function - particularly CRUD functions, functions that depend on the schema
  4512. * (for example by using drupal_write_record()), and any functions that invoke
  4513. * hooks.
  4514. *
  4515. * Instead, a simplified utility function should be used. If a utility version
  4516. * of the API function you require does not already exist, then you should
  4517. * create a new function. The new utility function should be named
  4518. * _update_N_mymodule_my_function(). N is the schema version the function acts
  4519. * on (the schema version is the number N from the hook_update_N()
  4520. * implementation where this schema was introduced, or a number following the
  4521. * same numbering scheme), and mymodule_my_function is the name of the original
  4522. * API function including the module's name.
  4523. *
  4524. * Examples:
  4525. * - _update_6000_mymodule_save(): This function performs a save operation
  4526. * without invoking any hooks using the 6.x schema.
  4527. * - _update_7000_mymodule_save(): This function performs the same save
  4528. * operation using the 7.x schema.
  4529. *
  4530. * The utility function should not invoke any hooks, and should perform database
  4531. * operations using functions from the
  4532. * @link database Database abstraction layer, @endlink
  4533. * like db_insert(), db_update(), db_delete(), db_query(), and so on.
  4534. *
  4535. * If a change to the schema necessitates a change to the utility function, a
  4536. * new function should be created with a name based on the version of the schema
  4537. * it acts on. See _update_7000_bar_get_types() and _update_7001_bar_get_types()
  4538. * in the code examples that follow.
  4539. *
  4540. * For example, foo.install could contain:
  4541. * @code
  4542. * function foo_update_dependencies() {
  4543. * // foo_update_7010() needs to run after bar_update_7000().
  4544. * $dependencies['foo'][7010] = array(
  4545. * 'bar' => 7000,
  4546. * );
  4547. *
  4548. * // foo_update_7036() needs to run after bar_update_7001().
  4549. * $dependencies['foo'][7036] = array(
  4550. * 'bar' => 7001,
  4551. * );
  4552. *
  4553. * return $dependencies;
  4554. * }
  4555. *
  4556. * function foo_update_7000() {
  4557. * // No updates have been run on the {bar_types} table yet, so this needs
  4558. * // to work with the 6.x schema.
  4559. * foreach (_update_6000_bar_get_types() as $type) {
  4560. * // Rename a variable.
  4561. * }
  4562. * }
  4563. *
  4564. * function foo_update_7010() {
  4565. * // Since foo_update_7010() is going to run after bar_update_7000(), it
  4566. * // needs to operate on the new schema, not the old one.
  4567. * foreach (_update_7000_bar_get_types() as $type) {
  4568. * // Rename a different variable.
  4569. * }
  4570. * }
  4571. *
  4572. * function foo_update_7036() {
  4573. * // This update will run after bar_update_7001().
  4574. * foreach (_update_7001_bar_get_types() as $type) {
  4575. * }
  4576. * }
  4577. * @endcode
  4578. *
  4579. * And bar.install could contain:
  4580. * @code
  4581. * function bar_update_7000() {
  4582. * // Type and bundle are confusing, so we renamed the table.
  4583. * db_rename_table('bar_types', 'bar_bundles');
  4584. * }
  4585. *
  4586. * function bar_update_7001() {
  4587. * // Database table names should be singular when possible.
  4588. * db_rename_table('bar_bundles', 'bar_bundle');
  4589. * }
  4590. *
  4591. * function _update_6000_bar_get_types() {
  4592. * db_query('SELECT * FROM {bar_types}')->fetchAll();
  4593. * }
  4594. *
  4595. * function _update_7000_bar_get_types() {
  4596. * db_query('SELECT * FROM {bar_bundles'})->fetchAll();
  4597. * }
  4598. *
  4599. * function _update_7001_bar_get_types() {
  4600. * db_query('SELECT * FROM {bar_bundle}')->fetchAll();
  4601. * }
  4602. * @endcode
  4603. *
  4604. * @see hook_update_N()
  4605. * @see hook_update_dependencies()
  4606. */
  4607. /**
  4608. * @} End of "defgroup update_api".
  4609. */