Quick tip: When /user and many other menu entries are missing

Recently, a friend of mine contacted me regarding his Drupal website; all website contents were missing and he even could not log into the website and check the situation.
He was in a great hurry and I checked the website shortly. Except the homepage -which looked to be set on /node- every other links were not accessible.
I asked him about any change in code and settings or anything unusual. He replied that he has not changed anything; just the fact that the web server was not fully responsive at the time of problem.

Chaos tool suite review: Dependents

It's been a long time that I want to write about Ctools. I have two good reasons: They are extremely useful -God bless Earl Miles, and it is very short on documentation.

Ctools have many tools to offer and I am going to explain some of them, one at a time. This time, I want to show you how to use Dependent tool: It make form items appear and disappear based upon the selections in another item.

Concept

When using Dependent tool, you have two kind of fields:

  • One field that it's selected value is observed by other fields.
  • One or more fields that are depended on the first field and appear or disappear upon the selected value of it.

Field that others are depended on its value can be one of these types: Radios, Checkbox and Select. Dependent fields can be anything, even markups.

Setting up subdomains on your local web server

One of the primary tasks for a web developer is to set up a development environment that resembles the production environment as much as possible.

When I was a beginner in web development, I used to create a subfolder for each new project in my web server documents root and put all my project files in there; the local address of my projects would look something like this: localhost/myproject/index.php. This approach was very awkward and used to cause many problems during deployment into production environment.

Now, when I start developing a new web project, first thing I do is setting up a local hostname for my project; something like myproject.localhost which maps to a folder separated from other ones.

In this article, I want to show you how you can create hostnames on your local computer. I assume that you use Apache web server. Please note that these hostnames are not necessarily subdomains of localhost. I like to create them like myproject.localhost; you can create something like mywebsite.com.dev or even google.com.

For creating a local hostname, you should do these steps:

  1. Map you hostname to an IP on your local computer or local network. It usually is 127.0.0.1. You can set different IPs as well.
  2. Create a virtual host in you Apache webs server an tell it where does this hostname point to.

Determining whether page queried through the HTTPS protocol

Have you ever wondered how you can determine the protocol f current page: HTTP or HTTPS?

There is an index in $_SERVER variable that helps you: $_SERVER['HTTPS'].
PHP manual says: "Set to a non-empty value if the script was queried through the HTTPS protocol". PHP manual adds this as well: "...when using ISAPI with IIS, the value will be off if the request was not made through the HTTPS protocol."
In other words, you need to check it like this:

  1. if (! empty($_SERVER['HTTPS']) and $_SERVER['HTTPS'] != 'off') {
  2.     // It's secure!
  3.     ...
  4. }

I am using this technique for a while and it has worked fine so far. However, somebody here mentioned that this technique does not work sometimes: "if you config your apache base on Name-based (More than one web site per IP address), this solution doesn't work" because "Name-based virtual hosting cannot be used with SSL secure servers because of the nature of the SSL protocol".

I am not aware of any other way to check the protocol. If you know a bullet-proof way to distinguish between HTTP and HTTPS, let me know.

How to create a new node in Drupal programatically

Here is an old story: How to create a node in Drupal programatically. This is my version.

Basics

First, if you have a general knowledge of a node structure, it would be much easier for you to create nodes by code. If you do not have such knowledge, don't worry; I will show you in this article.

First of all, a node is a container, containing data of your Story or Page etc. In code, it is represented as a PHP Object. All we do is creating an object and putting our data in it:

  1. global $user;
  2.  
  3. // Create new object.
  4. $node = new stdClass();
  5.  
  6. // Set desired data and metadata as members in object.
  7. $node->type = 'page';
  8. $node->uid = $user->uid;
  9. $node->title = 'node title';
  10. $node->body = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit...';
  11.  
  12. // Save the node
  13. node_save($node);

Note that if you don't set some values -Status or Promote to front page for example, they will be initialized by default values in Content type settings. If you want to overrode those values, set them as well:

  1. global $user;
  2.  
  3. $node = new stdClass();
  4. $node->type = 'page';
  5. $node->uid = $user->uid;
  6. $node->title = 'node title';
  7. $node->body = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit...';
  8.  
  9. $node->status = 0; // Not published
  10. $node->promote = 1; // Promote to front page
  11. $node->sticky = 1; // Sticky on top of list
  12.  
  13. node_save($node);

Here is a good place to have a look at a node structure. When you pass your node to function node_save(), it adds the necessary values to it and then save it. This function accepts your node as a refrenced variable, therefore after saving the node, you can have a look at it and see the complete structure of your new node using functions print_f() or dpm(). function dpm() is one of many useful utility functions in module Devel. If you have not installed Devel yet, I strongly recommend that do it right now! It is a must for every Drupal developer.

Syndicate content