Promet Knowledge Sharing

Archive for December, 2008

Ternary Operator

without comments

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 :)

Written by jun

December 11th, 2008 at 8:49 am

Posted in PHP, Tips

Tagged with

Drupal Comment Moderation

with 2 comments

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

  1. allow the author to enable or disable commenting on their post; and
  2. dictate if they want the comments to be approved or denied first before getting published in the public
Right now, only administrators are allowed to dictate if a node will accept comments or not. And it seems that Drupal core team does not have any plans on adding that, yet. There is however a module available that let users delete/edit/approve comments and overrides the permissions by the core. But, this still does satisfy my first requirement. Luckily I was toying with the Rules and CCK module and with them I was able to accomplish the first requirement without creating a new module or hacking another module — just pure administrative manipulation.

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:

  1. Add a CCK field
  2. Create a new rule
  3. Test
Lastly, this one is intended for D6 only. I haven’t tested in D5 and will never be.

Add a CCK field

This step will just mimic how an admin can enable/disable comments. Follow these steps.

  1. Go to Administer > Content management > Content types (this is located in the navigation block. each underlined text means a link in your drupal site)
  2. Click “manage fields” of the node type you wish. For this tutorial, we will use Page
  3. 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
  4. On the next page, copy and paste the following text in the Allowed values
    0|Disable
    1|Read
    2|Read/Write
  5. Optionally you can change other attributes and when your done click save button.
This will add a new field in your pages. Try to create or edit a page to see the added field in your edit form.

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!

  1. Go to Administer > Rules > Triggered rules
  2. Add a new rule (check the tab)
  3. Use the following information
    Label: Comment moderation
    Event: Content is going to be saved
    Click save changes button
  4. 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).
  5. In the next page, select Field has changed option and submit.
  6. 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.
  7. 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).
  8. 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.
  9. 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);
  10. Click save and we are done.
What will happen here is that when the node is created or updated (Step #3), it will check for a condition which we did in Step #5. If the condition is met, and that the field_comment’s value was changed, it will process the stated actions we added in Step #9. The code means it wants to replace the default comment status to the selected values of field_comment.

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.

What’s next?

There is no next! If you ever uninstalled Comment module, just deactivate this rule to save some processing time but it’s not required.

Written by rachel

December 11th, 2008 at 4:18 am

Posted in Drupal