-
Drupal 7 – Commerce Migration Class
Here’s a migration class I’ve been working on to import 8200 products. The biggest feature of this code is that it will automatically create a single product display node that groups all products who share the same “grouping identifier.” So, in my CSV import file I have a “grouping_identifier” column. If a product is available in ten colors, and they all share the same grouping identifier, a single product display will be created and each of these products will be referenced in it. This will result in a product on the frontend that allows ten different color choices. I don’t have the time right now to explain anything more… please leave a comment if you have a question.
-
Quick Tip: Adding a Reset button to a Drupal form
Using hook_form_alter or hook_form_FORM_ID_alter one can easily add a “Reset” button to a Drupal form.
In this example we’re actually trying to reset anything the user typed after the form loaded. This will not remove the default values.
Here’s a simple example (Drupal 6)
1234567891011121314/*** Implementation of hook_form_alter().*/function mymodule_form_alter(&$form, &$form_state, $form_id) {switch ($form_id) {case 'myform':$form['buttons']['reset_button'] = array('#type' => 'markup','#value' => '<input class="form-button" type="reset" value="Reset">','#weight' => 2000,);break;}}It may not be appropriate for you to put your new reset element into the buttons array. Use dpm() (part of the Devel module) to show what $form looks like. If you don’t understand this, and the code above isn’t working for you, you may try $form[‘reset_button’] = array….
Adjust the value of #weight to move the button around the form. dpm($form) will show you weights (if any) of existing elements so you can make educated decisions about the weight of your new field.
One last note about the ‘#type’ => ‘markup’ line: this is not a requirement, but I like to include it for clarity.
UPDATE:
Here’s what the field might look like for Drupal 7:
1234$form['reset'] = array('#type' => 'markup','#markup' => '<input class="form-submit" type="reset" value="Reset">',); -
Drupal Commerce Add To Cart Form Tweaks / Registration Improvements
In this example I’m showing how to improve the Add to Cart button for Drupal Commerce products to show differently based on whether or not users have already purchased a product, or whether the product is already in their cart.
I’m currently working on a site where users are registering for “programs,” which are synonymous with classes, courses, etc. I’m using the following modules (among others) in this example:
- Drupal Commerce
- Registration
- Commerce Registration
Please understand that when a user registers for a course, they are actually purchasing a product that has a referenced Registration entity.
-
Creating a Link on a Product Display Node to Edit a Referenced Product
Drupal Commerce is amazing. On a recent project I had the pleasure of working with it for the first time. I understood the link between products and product displays, but there were a few things that bothered me about the relationship. This example demonstrates a simple administrative UI modification to make it easier to edit a product display node’s referenced product. Notice I said product, not products. On this site, the requirement calls for a one-to-one relationship. Every product display will only have a single product. You’ll have to make modifications (to the block’s contextual filter (to support multiple arguments) and the PHP code (to pass back multiple product ids)) if you want to show multiple referenced products.
-
Disabling the “Read more” link if specific conditions are met
Here’s a simple way to remove the Read more link at the module level (in Drupal 7). Sometimes this is preferred over doing it at the theme level (because maybe we don’t want the link to appear no matter what theme is being used).
123456789/*** Implementation of hook_node_view().*/function mymodule_node_view($node, $view_mode) {// Remove the "Read more" links on Registrant Form teasersif ($node->type == 'regform' && $view_mode == 'teaser') {unset($node->content['links']['node']['#links']['node-readmore']);}} -
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