• Development,  Tech Tips

    Automatic Screenshots of Drupal Content

    In an earlier post I recommended webkit2png for automatically screenshotting a list of URLs. A lot of time has passed since that post, and I’ve discovered a more robust tool. Pageres is incredible, and it has a CLI and an api.

    I’ll let you discover, on your own, what the Pageres tool can do. I needed to take screenshots of all of the content types on a site, at all of the important resolutions. Here’s a quick Drupal function I threw together to get N number of random nodes per content type:

  • Development

    Disable Drupal’s default “user X has applied for an account” email

    By default Drupal sends an email to the site admin; it’s not editable (and you cannot disable it) from the account settings page.

    On a recent project I had to use Rules to send some emails when a user registers for an account. Because I was already sending emails via Rules, I needed to disable the stock admin email. Here’s a quick way to do this:

     

  • Development

    Allowing Users to Cancel Their Own Commerce Licenses

    UPDATE: I’ve written a contrib module called Commerce License Cancel to get the ball rolling to make this a community effort. Read on if you’d like to see how I got there.

    There’s been some talk in the issue queue for Commerce License Billing about needing a way for users to cancel their own licenses. Here are some pieces and parts that I’m using to achieve this. This is a work in progress and I’m really just posting here so I can provide a clean link in the issue queue. Sorry for the lack of detailed explanations; hopefully you still find this helpful.

    There’s already a method to “revoke” a user license. When you revoke a license, the recurring billing should close out automatically and the role granted (if it’s a licensed role) will be taken away from the user.

    The site I’m working on has just a few “membership plans” or levels that a user can purchase. For this reason, I could hardcode a few things. It’d take a LOT more work to make this stuff ready for the masses.

  • Development

    When Did Drupal Cron Last Run?

    Here’s a simple drush command to determine when Drupal cron (and node index cron) last ran:

     

  • Development

    Variable Types for Use in hook_rules_event_info()

    There is plenty of documentation out there explaining how to create custom rules events. What I had a tough time finding was a list of the variable types that are available for elements reacting on this event. I had seen examples of “text” and “user” but I really wanted to know what all of the types are.

  • Development

    Drupal AJAX Framework with Multiple Instances of a Form

    img20141125094913I recently encountered a unique issue with AJAX-ified form elements in Drupal. We’re building a site that shows the same form twice on a single page. I’m rendering both forms in two separate blocks; one appears at the top of the page, and one appears at the bottom.

    The Select a Location field has an ajax callback to replace the the Select a Program select list with programs available at the chosen location. Initially, the top form worked as expected, but the bottom form would never get an updated Select a Program field. The AJAX callback seemed to run, but it would not replace the select list.

    After fixing (by adding a unique, unchanging identifier, both forms performed as expected; changing the Select a Location dropdown updates the Select a Program dropdown, and the two forms do not interfere with one-another.

    Originally I tried using $form['#id']  because it looked cleaner, but it didn’t work correctly. The Select a Program list would update, but it’d have the same values every time, no matter which location was chosen.

  • Development

    Sending HTML Emails from a Drupal 7 Site

    It was a little more difficult than I’d hoped to track down the proper method for sending HTML emails from Drupal 7. I’m working on a problem with Webform, so Webform emails were my primary concern. I’m not going to spend any time explaining the solution. I encourage you to look into the modules if you’re interested in learning more.

    1. Download and enable mailsystem
    2. Download and enable htmlmail
    3. Visit /admin/config/system/mailsystem
    4. Choose “HTMLMailSystem” as the mail class for all applicable “modules”
      1. To add a module (e.g., Webform), open the “New Setting” at the bottom and choose:
        1. Module = Webform
        2. Key = leave blank
      2. Save settings
      3. Update that module to use the HTMLMailSystem
    5. Try it out.
  • Development

    Devel Generate Text Settings

    Devel Generate, which is part of the Devel module, makes it really simple to create dummy entities in Drupal. It’ll create everything from users to taxonomy terms to nodes. It does a great job of populating most field types (including images). Sometimes it’d be nice to have more control over the output that it’s generating. In particular, text fields often get populated with too much text.

    I’ve written a module to solve this dilemma.

    The Devel Generate Text Settings module lets you control the length and format (sentence structure vs. title) of text and long text fields. It also lets you easily add a prefix to the title of the nodes being generated.

    One problem you may have run into is for fields that need a limited amount of words or characters. Imagine you have a Father’s Name field. Most of the time you’d expect to see two words (first name and last name). Devel Generate will almost certainly generate more than two words for this field. It may even generate 40! That’s not desirable. We need the dummy content to appear as close to the real thing as possible.

    Devel Generate Text Settings gives you the ability to tell Devel Generate to stay within a certain word count, or even hit an exact word count. It also allows you to set the “treat as title” value to true, which removes any punctuation from the generated text and capitalizes the first letter of each word.

    Here is what the rule might look like for the field in this example:

    The breakdown is: when generating content for the field_father_name field, generate either one or two words. Exclude punctuation and capitalize the first letter of each word.

    This is just a sandbox project for now, and it only works with nodes.

    Here’s a screenshot of the admin at the time of writing:

    img20141016155815

  • Development

    Pull Quotes in Drupal 7 – Using hook_filter_info()

    I recently wrote a post called Pull Quotes Shortcodes via Custom Filters in Drupal 7 . This was a good solution, but I decided I wanted a little more flexibility in my filter (like being able to specify attributes in any order) and I wanted to get rid of the required [/pullquote] closing tag. Custom Filter has its limitations (and working through the gui would’ve been a PITA), so I fired up Vim and made a custom filter using hook_filter_info().

    Here is part of my original description of what I needed:

    I’ve been working on a Drupal 7 site that requires some nicely-formatted pull quotes. The site editors need the ability to add them to their content easily through the “filtered” text fields throughout the site (e.g., body).