-
Copying Values Between Fields on Thousands of Entities
In a nutshell, I had a List (text) field that I needed to convert to a Text field (so that I wasn’t locked in to specific values anymore). Instead of trying to modify the field to convert it from one type to another, I decided to create a new Text field and populate it with the data from the List field (modifying it a bit first). Here’s the script I used to do this. Admittedly it could be cleaner, more efficient, etc. but it’ll show you a few things, and it got the job done for me.
If you choose to run with
drush scr update_nodes.php
you can leave out the top 3 bootstrap lines. -
Creating Field Groups in a Custom Module
If you need your module to add a fieldgroup to your Drupal 7 site, follow this quick and easy process: Also, if you’re looking to create fields, see this related post.
- Build the fieldgroup through the “manage fields” UI.
- Modify the code below to include a proper $fieldgroup_name, $entity_type and $bundle_name. I needed to add a field to the user profile, so I set the $entity_type to “user” and the $bundle_name to “user”.
1234567891011$fieldgroup_name = ''; // e.g., group_author_info$entity_type = ''; // e.g., node$bundle_name = ''; // e.g., storyinclude_once DRUPAL_ROOT . '/includes/utility.inc';$group = field_group_load_field_group($fieldgroup_name, $entity_type, $bundle_name, 'form');$group_var = '$' . $fieldgroup_name . ' = ' . substr(drupal_var_export($group), 9) . ";\r";$group_var = preg_replace("/ 'id'.*,\n/", '', $group_var); // remove the ID property$group_var = preg_replace("/ 'export_type'.*,\n/", '', $group_var); // remove the export_type property$output = $group_var . 'field_group_group_save($' . $fieldgroup_name . ');';drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>');
-
Creating Fields in a Custom Module
I’m going to be very brief here, because Joel Stein covers this beautifully. Also, if you’re looking to create fieldgroups, see this related post.
If you need your module to add a field to your Drupal 7 site, follow this quick and easy process:- Build the field through the “manage fields” UI.
- Modify the code below to include a proper $field_name, $entity_type and $bundle_name. I needed to add a field to the user profile, so I set the $entity_type to “user” and the $bundle_name to “user”.
123456789101112$field_name = ''; // e.g., field_story_pagecount$entity_type = ''; // e.g., node$bundle_name = ''; // e.g., story$info_config = field_info_field($field_name);$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);unset($info_config['id']);unset($info_instance['id'], $info_instance['field_id']);include_once DRUPAL_ROOT . '/includes/utility.inc';$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";drupal_set_message("<textarea rows=30 style=\"width: 100%;\">" . $output . '</textarea>');
-
WordPress Plugin: Search Regex
I wanted to quickly mention this plugin as it saved me a bit of time just now. While I worked on this new blog I set the development site up at wordpress.agileadam.com. When I switched the agileadam.com domain over to point to this new WordPress site all of my images stopped working (because the img tags pointed to wordpress.agileadam.com urls; shouldn’t they be relative anyway!? I’ll look into that later).
-
QuickLook Slow in Mavericks?
Is your QuickLook painfully slow after upgrading to Mavericks? Until there’s a solution from Apple, if you’re on a mac with a Retina display try this:
- Open the Applications directory in Finder
- Right-click the application Preview
- Select Get Info.
- Enable/check the Open in Low Resolution.
Source: http://wccftech.com/how-to-fix-quick-look-in-os-x-mavericks/
-
Add Links to Anchors Automatically
The following snippet is something I used recently to add (below the current item in a menu) links to any anchors found in the content.
1234567$("#content a[name]").each(function() {if ($("#page-anchors").length === 0) {$(".menu-block-1 li.active:last").append("<ul id='page-anchors'>");}var aname = $(this).attr("name");$("#page-anchors").append($("<li><a href='#" + aname + "'>" + aname + "</a></li>"));});Honestly I could probably create the <ul> outside of the each() loop and save having to check whether it exists with each iteration. Also, it’s probably not necessary to create the aname variable, but the overhead here is minimal.