-
Programmatically displaying an image using an imagecache preset
The following illustrates one way to render an image using an Imagecache preset. Naturally, if your imagefield has multiple uploads you could wrap this in a foreach.
1234$path = $node->field_pgallery_images[0]['filepath'];$alt = $node->field_pgallery_images[0]['data']['alt'];$title = $node->field_pgallery_images[0]['data']['title'];print theme('imagecache', ‘presetname’, $node->field_pgallery_images[0]['filepath'], $alt, $title, $attributes); -
States in key|value format
The following is a list of the United States in key|value format. It’s useful when creating a select list in Drupal.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758AL|AlabamaAK|AlaskaAS|American SamoaAZ|ArizonaAR|ArkansasCA|CaliforniaCO|ColoradoCT|ConnecticutDE|DelawareDC|District of ColumbiaFL|FloridaGA|GeorgiaGU|GuamHI|HawaiiID|IdahoIL|IllinoisIN|IndianaIA|IowaKS|KansasKY|KentuckyLA|LouisianaME|MaineMH|Marshall IslandsMD|MarylandMA|MassachusettsMI|MichiganMN|MinnesotaMS|MississippiMO|MissouriMT|MontanaNE|NebraskaNV|NevadaNH|New HampshireNJ|New JerseyNM|New MexicoNY|New YorkNC|North CarolinaND|North DakotaMP|Northern Marianas IslandsOH|OhioOK|OklahomaOR|OregonPW|PalauPA|PennsylvaniaPR|Puerto RicoRI|Rhode IslandSC|South CarolinaSD|South DakotaTN|TennesseeTX|TexasUT|UtahVT|VermontVI|Virgin IslandsVA|VirginiaWA|WashingtonWV|West VirginiaWI|WisconsinWY|Wyoming -
Countries in key|value format
The following is a list of countries in key|value format. It’s useful when creating a select list in Drupal.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241AF|AfghanistanAL|AlbaniaDZ|AlgeriaAS|American SamoaAD|AndorraAO|AngolaAI|AnguillaAQ|AntarcticaAG|Antigua And BarbudaAR|ArgentinaAM|ArmeniaAW|ArubaAU|AustraliaAT|AustriaAZ|AzerbaijanBS|BahamasBH|BahrainBD|BangladeshBB|BarbadosBY|BelarusBE|BelgiumBZ|BelizeBJ|BeninBM|BermudaBT|BhutanBO|BoliviaBA|Bosnia And HerzegovinaBW|BotswanaBV|Bouvet IslandBR|BrazilIO|British Indian Ocean TerritoryBN|Brunei DarussalamBG|BulgariaBF|Burkina FasoBI|BurundiKH|CambodiaCM|CameroonCA|CanadaCV|Cape VerdeKY|Cayman IslandsCF|Central African RepublicTD|ChadCL|ChileCN|ChinaCX|Christmas IslandCC|Cocos (keeling) IslandsCO|ColombiaKM|ComorosCG|CongoCD|Congo, The Democratic Republic Of TheCK|Cook IslandsCR|Costa RicaCI|Cote D'ivoireHR|CroatiaCU|CubaCY|CyprusCZ|Czech RepublicDK|DenmarkDJ|DjiboutiDM|DominicaDO|Dominican RepublicTP|East TimorEC|EcuadorEG|EgyptSV|El SalvadorGQ|Equatorial GuineaER|EritreaEE|EstoniaET|EthiopiaFK|Falkland Islands (malvinas)FO|Faroe IslandsFJ|FijiFI|FinlandFR|FranceGF|French GuianaPF|French PolynesiaTF|French Southern TerritoriesGA|GabonGM|GambiaGE|GeorgiaDE|GermanyGH|GhanaGI|GibraltarGR|GreeceGL|GreenlandGD|GrenadaGP|GuadeloupeGU|GuamGT|GuatemalaGN|GuineaGW|Guinea-bissauGY|GuyanaHT|HaitiHM|Heard Island And Mcdonald IslandsVA|Holy See (vatican City State)HN|HondurasHK|Hong KongHU|HungaryIS|IcelandIN|IndiaID|IndonesiaIR|Iran, Islamic Republic OfIQ|IraqIE|IrelandIL|IsraelIT|ItalyJM|JamaicaJP|JapanJO|JordanKZ|KazakstanKE|KenyaKI|KiribatiKP|Korea, Democratic People's Republic OfKR|Korea, Republic OfKV|KosovoKW|KuwaitKG|KyrgyzstanLA|Lao People's Democratic RepublicLV|LatviaLB|LebanonLS|LesothoLR|LiberiaLY|Libyan Arab JamahiriyaLI|LiechtensteinLT|LithuaniaLU|LuxembourgMO|MacauMK|Macedonia, The Former Yugoslav Republic OfMG|MadagascarMW|MalawiMY|MalaysiaMV|MaldivesML|MaliMT|MaltaMH|Marshall IslandsMQ|MartiniqueMR|MauritaniaMU|MauritiusYT|MayotteMX|MexicoFM|Micronesia, Federated States OfMD|Moldova, Republic OfMC|MonacoMN|MongoliaMS|MontserratME|MontenegroMA|MoroccoMZ|MozambiqueMM|MyanmarNA|NamibiaNR|NauruNP|NepalNL|NetherlandsAN|Netherlands AntillesNC|New CaledoniaNZ|New ZealandNI|NicaraguaNE|NigerNG|NigeriaNU|NiueNF|Norfolk IslandMP|Northern Mariana IslandsNO|NorwayOM|OmanPK|PakistanPW|PalauPS|Palestinian Territory, OccupiedPA|PanamaPG|Papua New GuineaPY|ParaguayPE|PeruPH|PhilippinesPN|PitcairnPL|PolandPT|PortugalPR|Puerto RicoQA|QatarRE|ReunionRO|RomaniaRU|Russian FederationRW|RwandaSH|Saint HelenaKN|Saint Kitts And NevisLC|Saint LuciaPM|Saint Pierre And MiquelonVC|Saint Vincent And The GrenadinesWS|SamoaSM|San MarinoST|Sao Tome And PrincipeSA|Saudi ArabiaSN|SenegalRS|SerbiaSC|SeychellesSL|Sierra LeoneSG|SingaporeSK|SlovakiaSI|SloveniaSB|Solomon IslandsSO|SomaliaZA|South AfricaGS|South Georgia And The South Sandwich IslandsES|SpainLK|Sri LankaSD|SudanSR|SurinameSJ|Svalbard And Jan MayenSZ|SwazilandSE|SwedenCH|SwitzerlandSY|Syrian Arab RepublicTW|Taiwan, Province Of ChinaTJ|TajikistanTZ|Tanzania, United Republic OfTH|ThailandTG|TogoTK|TokelauTO|TongaTT|Trinidad And TobagoTN|TunisiaTR|TurkeyTM|TurkmenistanTC|Turks And Caicos IslandsTV|TuvaluUG|UgandaUA|UkraineAE|United Arab EmiratesGB|United KingdomUS|United StatesUM|United States Minor Outlying IslandsUY|UruguayUZ|UzbekistanVU|VanuatuVE|VenezuelaVN|Viet NamVG|Virgin Islands, BritishVI|Virgin Islands, U.s.WF|Wallis And FutunaEH|Western SaharaYE|YemenZM|ZambiaZW|Zimbabwe -
Custom Voting API Calculation
The Voting API is really nice when used in conjunction with a module like Fivestar. It takes all of the complexity out of setting up a voting/rating system. On occasion, however, you need it to do some things that aren’t built in. Using the VotingAPI’s API, I’ll show you how to trigger a function (send an email, write a message to the screen, etc.) when a piece of content receives a fifth vote of four stars or greater. That is, a node may have four votes of five stars, and two votes of two stars. As soon as the next greater-than-four vote goes in, we want to do something about it. In our case, the client wants to receive an email when a node is popular, and has had five four-star-or-greater votes.
-
Show Menu Description Field in Node Add/Edit Form
Many components in Drupal have an optional title or description field. This text usually displays when a user hovers over an item. We had an interesting request: a client wanted to have a box that displayed text which would change every time you roll over a menu item. This can be accomplished using just a small amount of jQuery, and Drupal’s built-in menu handling. The menu system in Drupal can handle descriptions on each menu item, however you have to do this through the menu admin. The problem is that the client needed to be able to edit these descriptions at will.
-
Views Taxonomy: Get Terms with Associated Nodes
This example serves as both an example of how to alter a Views2 query, as well as how to use the get_terms_by_count() function I’ve written.
Unfortunately there is not (at present) a Views2 taxonomy filter that lets you “Get only terms with at least X associated nodes.” We had a client request that terms without associated nodes be hidden. This was actually more complex than it sounds, but the solution led me to a whole new level of Views2 understanding. Views2 has a hook called hook_views_query_alter() that lets you alter a Views2 query before it is executed. This is exactly what we needed to do in order to only pull terms with associated nodes. Specifically, we needed to add an additional WHERE clause to the query.
-
Rebuilding the Drupal Navigation Menu (Automatically)
The following steps will get you a completely rebuilt Navigation menu.
- Clear your cache
- Backup your database
- In the database, remove any records in {menu_links} where menu_name = navigation
- Create a file somewhere in your site’s directory structure
- Add this code to that file and then save it
123456789<?php//uncomment the next two lines to automatically use the admin user (not advised)//global $user;//$user-?-->uid = 1;require_once './includes/bootstrap.inc';drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);menu_rebuild();?> - Tell Drupal to ignore this file using this method.
- Browse to the file in your web browser (it will run quickly, with no indication of completion). Wait for the page to stop loading, then check your navigation menu settings in the admin.
- When finished, delete or make-inaccessible the PHP file you just made!
-
$user Conditionals
This is one example of performing an action based on whether or not the current user belongs to a certain role or roles. Note that the admin user (UID #1) matches as well.
12345678global $user;$roles = array_values($user->roles);if (in_array('admin', $roles)) || in_array('developer', $roles)) || $user->uid == 1) {print $content;}else{header("location:/not-allowed");}Note: You may want to try using drupal_goto() (or drupal_access_denied() in this case) instead of a header redirect.
-
Passing Arguments to Drupal Blocks
You can pass any number of arguments to a Drupal block by providing default arguments. The screenshot below shows what an argument might look like in Views2.
-
Open All External Links in a New Window/Tab using jQuery
123$(document).ready(function() {$("a[href^='http']").not("[href*='mysite.com']").attr('target','_blank');});
123$(document).ready(function() {$("a[href^='http']").not("[href*='mysite.com'],[href*='mysite2.com']").attr('target','_blank');});