Pages

Wednesday, February 17, 2016

Drupal Text Formats and Content for Test Nodes

When building a Drupal-based website or theme, you will invariably create some test nodes of a content type that has at least one text area field. You should then add some example text, to see how those fields will look on the website. But beyond those nodes focused on styling, you may find it necessary or advisable to create a large number of additional test nodes of that content type. When you begin this process, you may wonder whether it would be best to add placeholder text (such as the venerable "Lorem ipsum") or leave those fields empty. Also, are there any ramifications of choosing one text format or another?
If a text area contains no content, then no text format is associated with that particular node's text area, and changes to the text format setting (made through the user interface) are not retained. However, if placeholder text is added to the field, then users who do not have permission for the chosen text format, will naturally not be able to edit the text. Consequently, you or someone else would be forced to change the text formats on all of the problematic nodes — manually or with database commands. On the other hand, if you leave those text areas empty, then those users can add whatever text they want, and specify any of the text formats for which they have permissions.

Hardcoding the Temporary Directory Path

When working on a local copy of a site that also has a production, stage, and/or development environment on another server, it can be handy to hard-code the "Temporary directory" file path as found on admin/config/media/file-system
The reason is so that when you restore a copy of the database from another server to your local machine, you don't have to redo the temporary directory setting. For example, on most sites we build, we set up a shared development environment on WebEnabled (shameless plug) where multiple developers are often configuring different parts of the site. When I want to work locally, I like to have an up-to-date copy of the database. I use the Backup and Migrate module or Drush to copy the database from the shared development environment to my local machine.
To hard-code the temporary directory path of your local site, simply add the following line to the bottom of your local settings.php file:
$conf['file_temporary_path'] = '/tmp';
Where "/tmp" is the your local temporary directory path.

Get notified of new comments with Rules

I know there are modules to handle this, but Rules can do it all:
If you visit admin > config > workflow > rules on your site, start a new Rule like so:
Name: "Notify of new comment"
React on Event: After saving a new comment
I normally tag the rule with the name of the site, like "drupaleasy".
Next add a new Condition:
Select the condition to add: User > User has role(s)
User > Data selector: "comment:author"
Roles > Value: administrator
check the Negate box
This looks at the role of the person who just left the new comment. If he or she is an administrator, this rule wil not fire. Chances are, if an admin left a comment, you don't want to know about it. On most sites I don't add this condition, but on a single-user site like a blog, this rule will make a lot of sense.
Next add an Action - this is what you want to happen when a new comment is created (that was not added by the administrator):
System: Send mail
To > Value: "[site:mail]"
Subject > Value: "New comment on [comment:node]"
Message > Value:
[site:url]admin/content/comment/approval
[comment:url]
[comment:title] - [comment:node]
at [comment:created] by [comment:author]
[comment:mail] - [comment:homepage]
[comment:body]
[comment:edit-url]
From > Value: "[comment:mail]"
On my sites, Anonymous comments have to pass approval - we just get too much spam otherwise. Luckily, now that we have this rule in place, we can think about lifting the approval gate so comments appear instantaneously while still helping us cut down on spam.
Here is an exported version of the rule:
{ "rules_notify_of_new_comments" : {
    "LABEL" : "Notify of new Comments",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "drupaleasy" ],
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "NOT user_has_role" : {
          "account" : [ "comment:author" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[site:mail]",
          "subject" : "New comment on [comment:node:title]",
          "message" : "[site:url]admin\/content\/comment\/approval\r\n[comment:url]\r\n\r\n[comment:title]\t - [comment:node]\r\nat [comment:created] by [comment:author]\r\n[comment:mail] - [comment:homepage]\r\n\r\n[comment:body]\r\n\r\n[comment:edit-url]",
          "from" : "[comment:mail]"
        }
      }
    ]
  }
}

Enabling Drupal 7 'www' Redirection on a Local Host

Drupal 7's HTTP access file, .htaccess, contains some URL rewriting code for redirecting all site visitors to the domain name starting with "www.", in case they try to go to the domain name without the prefix. For instance, you might want all visitors to go to www.example.com, and never example.com, for SEO purposes.
This is achieved by uncommenting the code on lines 81-82:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This works fine when used on a remote server, but not on a local server that is not set up to handle the prefix. For instance, if you have a local installation of Drupal at http://localhost/my_site/, then the rewrite code will change that into an invalid URL: http://www.localhost/my_site/
This problem can be fixed by adding the following line above the RewriteRule:

RewriteCond %{HTTP_HOST} !^localhost$ [NC]

Or, more simply, the existing RewriteCond line can be modified:

RewriteCond %{HTTP_HOST} !^(www\.|localhost$) [NC]