Archive for December, 2008
Ternary Operator
Usually we use the following snippet of code to assign a value in a variable given a condition:
<?php
if (cond) {
v = value1
} else {
v = value2
}
?>
This code can be expressed using the ternary operator.
<?php
v = cond ? value1 : value2;
?>
Of course we can use the ternary operator with nested conditions.
<?php
v = cond ? (cond1 ? value11 : value12) : (cond2?:value21:value22);
?>
But that isn’t really an advisable option
<?php
if (cond) {
v = value1
} else {
v = value2
}
?>
This code can be expressed using the ternary operator.
<?php
v = cond ? value1 : value2;
?>
Of course we can use the ternary operator with nested conditions.
<?php
v = cond ? (cond1 ? value11 : value12) : (cond2?:value21:value22);
?>
But that isn’t really an advisable option
Drupal Comment Moderation
I’ve been working on a project using Drupal as our “framework”. One of our requirements is to allow our authors to moderate comments. What I mean by comment moderation is
In order to get us rolling, we need 3 modules: Rules, CCK and PHP filter. PHP filter is already included in Drupal 6 (or D6 for short) so you have to download the first 2 modules. There are 3 major steps to fullfill this:
You might also notice that in your page view, you will see the Comment option has been displayed. We can hide this in the admin. Let’s go back again to our Content type administration area (Administer > Content management > Content types). Click manage fields and then click Display fields tab. Just change the Teaser and Full node column of your field_comment to <Hidden>. It will hide the field to the page view. Check again.
- allow the author to enable or disable commenting on their post; and
- dictate if they want the comments to be approved or denied first before getting published in the public
In order to get us rolling, we need 3 modules: Rules, CCK and PHP filter. PHP filter is already included in Drupal 6 (or D6 for short) so you have to download the first 2 modules. There are 3 major steps to fullfill this:
- Add a CCK field
- Create a new rule
- Test
Add a CCK field
This step will just mimic how an admin can enable/disable comments. Follow these steps.- Go to Administer > Content management > Content types (this is located in the navigation block. each underlined text means a link in your drupal site)
- Click “manage fields” of the node type you wish. For this tutorial, we will use Page
- Add a new field with the following attributes (you can change this later if you wish)
Label: Comment moderation
fieldname: comment
Type of data to store: Text
Form element to edit the data: Check boxes/radio buttons - On the next page, copy and paste the following text in the Allowed values
0|Disable
1|Read
2|Read/Write - Optionally you can change other attributes and when your done click save button.
Create a new rule
This one will be a bit long but if you are already used to rules, then it is a piece of cake!- Go to Administer > Rules > Triggered rules
- Add a new rule (check the tab)
- Use the following information
Label: Comment moderation
Event: Content is going to be saved
Click save changes button - You have created a new rule! But we are not done yet. Let’s add some conditions (click the add a condition link at the Rule elements group).
- In the next page, select Field has changed option and submit.
- Then we can start adding more information regarding this condition. The only important fields you should fill up is the Field. Make sure to select our newly created CCK field - field_comment. Do not change anything except for the label if you want and then click the Save button.
- You are then redirected to the Comment moderation edit page. Let’s add a new action (click the add an action link at the Rule elements group).
- In the next page, select Execute custom PHP code (that is why we need the PHP filter so we can write some code) and click Forward button.
- Next, edit the label to your liking and in the PHP Code textarea, copy and paste the following:
// change the comment moderation status
$node->comment = $node->field_comments[0]['value'];
return array(”node” => $node); - Click save and we are done.
Testing
We are now ready to test it. Create a new page or edit the page you created before. Try to disable and enable the values and save. It should be working. If not, let me know the problem in the comment box.You might also notice that in your page view, you will see the Comment option has been displayed. We can hide this in the admin. Let’s go back again to our Content type administration area (Administer > Content management > Content types). Click manage fields and then click Display fields tab. Just change the Teaser and Full node column of your field_comment to <Hidden>. It will hide the field to the page view. Check again.
