Drupal Archives - Page 2 of 3 - developed.be

Instead of logging to the database, it could make more sense to log to the operating system.

Why?

Pro

  • The OS has already a great way of maintaining logs, so there isn’t really any need to have a separated database logging.
  • The options to maintain or report logs with the OS are much larger than the default Drupal database logging system.
  • Database logging eats away performance from your server, certainly when Drupal writes tons of logfiles each day.

Con

  • Any shared hosted website can’t use this option, because they have no access to the OS’ logsystem.
  • If your website is maintained by a so called “webmaster” or a “not so technically equipped person”, it may be easier to log to the database, because nontech persons would generally prefer to access the logs through the Drupal admin interface instead of using some dodgy Unix command. Of course, you could ask yourself if a nontech person is anything with the Drupal logs in the first place.

Continue reading “Write Drupal logs to rsyslog instead of to dblog”…

This function will print a large list of all the variables that are declared in the current scope:

<?php print_r(get_defined_vars()); ?>

An interesting variable is $node, but it is not always accessible.

Once you have created a field in Drupal, the fieldname will haunt you forever. A spelling mistake will keep living, and refactoring is out of the question.

It is however possible to change a fieldname in Drupal 6, but it might cost you a couple of hours work and a hard focus.

Background

Drupal stores the fieldname in every thinkable place:

  • column-names
  • table-names
  • row-values

The obvious content-field-tables are an easy task, you can even do that manually. But Drupal also stores the fieldname in many other not-so-evident places, in views for example where fieldnames are stored as serialized data.

An all working script to change the fieldname is not evident (a reason why the possibility is not included by default in Drupal). There is a module that did an approach to change a fieldname, but in my option it’s best to do it all manually with a checklist.

Step by step guide

I made a step by step guide about how to change the machine name of a Drupal-6 field.

1) Backup

Backup your entire database. Test this script on your local machine or on a dev-server, not in a live environment. There is no guarantee that it won’t break your database.

2) Automatic script to change evident table names and column names

I made a php-script to make the task easier.

The script:

  • changes the fieldname in the most common places in the database.

Continue reading “How to change the machine name of a content field in Drupal6″…

I wanted to make a list of “related nodes” based on the term that is attached to the node.

Say I have 10 nodes that have the term “fruit”. With every fruit-node I want to display 5 links to other fruit-nodes.

I work with panels and views.

This turned out more time-consuming then I thought because the way to do it is not straight forward.

  1. Make a new view
  2. Add “taxonomy term id” as argument. Select “hide view” is the argument isn’t provided.
  3. Add a couple of fields to display (eg: title, teaser).
  4. Save the view. Make sure your caches are cleared.
  5. Make or edit a page (with the panels module through: Administer › Site building › Pages).
  6. Make or edit a variant.
  7. We need to add a new relationship to the variant. This will make it possible to parse the term id  to the view.
    1. Click on the tab “context” in the variant.
    2. Add a relationship. Select  “Term from node” in the dropdown and click the “Add relationship” button.
    3. A new window will open. Select the correct vocabulary (in my example: the vocabulary where “fruit” belongs to). You might want to give it a decent name so you can recognise it later.
    4. Click “ok”.
  8. In the “content” tab, add a new pane to the panel. (click the gear icon and select “add content”)
  9. Select the view you’ve just created (under the “views” tab on the right) and select the “default” view. (do not make a “content pane” in the view because this does not work that way).
  10. Normally it will show the views’ argument (term id) with a dropdown under it. In the dropdown select the term id of the relationship we created in Step 7.
  11. Click update & save. This should do.

Our main RSS-feed at DeWereldMorgen.be is the most requested page next to our homepage.

It seems logic, think of how many rss-readers hourly check the feed. And, think of how many cpu and RAM that consumes, certainly with a fat system like Drupal.

An RSS-feed is easy to make in PHP. All you need is one custom query and a decent library like the Universal Feed Generator to generate the XML.

Create stripped version of Drupal setup

I used the minimal code that is needed to work in the Drupal framework. So I made a blank php-file in my www-root with this code:

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

This code has the security and functions of Drupal, but without the menu’s, theme’s, and a lot of module hooks. You don’t need a menu or a theme to create an RSS-feed, do you?

So I created my RSS-feed with just this in my php-file:

  • the drupal_bootstrap function
  • the Universal Feed Generator library included
  • one query with db_query()

With cache

Let’s see performance wise. This is the report of a feed generated based on a user’s blog and articles. Memcache was not cleared before execution. Stats are generated with XHProf.

Normal setupMy stripped setup
Number of function calls115.20018.400
Consumed RAM126 MB62 MB
Total execution time1.169 MS462 MS
Number of database queries3315
Query execution time137 MS80 MS

Performance wise, my solution is twice as fast.

Without cache

These stats are generated when caches were cleared.

Normal setupMy stripped setup
Number of function calls3,284,34383,330
Consumed RAM172 MB76 MB
Total execution time9,131 MS1,000 MS
Number of database queries926131
Query execution time769 MS183 MS

9x as fast. 7x less queries.

Of course, this is just for the first run, but still.

More soon. Please discuss this idea.

 

The short answer:

It’s not possible

The long answer:

In a simple user form it is possible to add a single on/off checkbox.

  $form['thermometer_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show the thermometer'),
    '#options' => array(0 => t("no"), 1 => t("yes")),
  );

But, it’s not possible to provide a default value (checked/unchecked) with a single chechbox. This causes difficulties when you want to show the saved value of a user.

A simple solution is to use a “yes/no radiobutton list” instead of a single checkbox.

  $form['thermometer_enabled'] = array(
    '#type' => 'radios',
    '#title' => t('Show the thermometer'),
    '#options' => array(0 => t("no"), 1 => t("yes")),
    '#default_value' => variable_get('thermometer_enabled', 0),
  );

In this example the result is saved in the variable thermomoeter_enabled and has the default value of 0 (false).

  • Published:August 21st, 2012
  • Category:Drupal
  • 1 Comment

Ever wondered which module gets executed first?

By default, every module has a weight of zero. If you want a module or its hooks to be executed first, you have to give it a lower weight (eg: -5). If you want a module to be executed after the others, you have to give it a higher weight (eg: 5).

Note that some modules (like Devel) have a standard higher weight.

In Drupal 6, the only way to change the weight is through sql.

db_query("UPDATE {system} SET weight=%d WHERE name ='%s'", $your_weight, $your_module_name);

More info and a nice list of weights on Drupal.org

In case you’ve installed Varnish but not Pressflow (for Drupal 6), following scenario may happen:

  1. User A logs is, gets sessionid A
  2. User A changes something and loads a new page
  3. While loading the new page, a js or css-file is being downloaded from Varnish (example: /sites/default/files/js/js_79eb17289b3a88ec931b6f4bdb728282.js)
  4. The next file that is being downloaded is a jpg. This file doesn’t come from the Varnish cache and gives a new sessionid to the user (sessionid B)
  5. The requested page is being served correctly because it was requested with sessionid A. The user is unaware that he has a new sessionid because it happened during the loading of the page elements.
  6. The user clicks on another page and sends a new request with sessionid B.
  7. Drupal checks sessionid B and sees that it the session belongs to an anonymous user. Result: the user gets an “Access Denied” and is logged out.

Solution: install Pressflow. It will stop giving sessionids to the client.

(this post only applies if you have installed Varnish)

This one gave me headache: the Mollom module in Drupal 6 returned a 401: unauthorized. However: there was no firewall, no incorrect key, and I really got a response back from the server. Top of the bill: it worked on my dev-machine but not on the server.

Till I noticed the time of log reports didn’t match the real time. It was 17:15 while Drupal said 16:58. A time difference of almost 20 minutes. I set the time correct and ta-dah: Mollom works.

Key manual: http://techportal.ibuildings.com/2009/12/01/profiling-with-xhprof/

Install XHProf

I made sure xhprof-0.9.2 was installed inside the www-directory of the webserver.

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd ./xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
sudo make
sudo make install
sudo make test

The php-config file on my machine was located in /usr/local/php-config . Php-config is a file that lists the location of your php-installation. Xhprof needs it for its configuration. If you can’t find the file, you can simply execute ./configure without the parameter.

If phpize is not found, you have to install php5-dev

sudo apt-get install php5-dev

The first time, “make install” and the “make test” both failed with me, but xhprof worked nevertheless.

Set up PHP

In php.ini I set up this at the end of the file:

[xhprof]
extension=/path/to/xhprof-0.9.2/extension/modules/xhprof.so
xhprof.output_dir="/var/tmp/xhprof"

Create the directory: /var/tmp/xhprof

xhprof will keep its logfiles in there (files that are needed for the page analysis)

Set up your webserver

Create an entry for /path/to/xhprof-0.9.2/ in your webserver’s configuration (point xhprof.localhost to the /path/to/xhprof-0.9.2/. If local, setup your hostfile correctly (eg: 127.0.0.1  xhprof.localhost)

Restart php and your webserver.

Set up Drupal

When using Drupal, install Devel, and activate the XHProf settings (admin/settings/devel) and point to /path/to/xhprof-0.9.2/ and the path to the website: http://xhprof.localhost/xhprof_html

Devel will create a link at the end of each page that points to XHProf.

Troubleshooting

If you get the error that the function xhprof_enable is unknown, then it’s because XHProf wasn’t properly installed. Try to install it again and make sure you have restarted php.

If the link in Drupal to XHProf doesn’t work, check if the run parameter in the url is supplied. If not, try to install XHProf again and make sure you have restarted php. A typical error for this is: “No XHProf runs specified in the URL.

For errors concerning php-config and phpize, see above.

« Previous PageNext Page »