Archive for the ‘CMS’ Category

Keep Joomla 1.0.x compatible after upgrading PHP 5.2 to 5.3 in Plesk

Saturday, June 4th, 2011

PHP 5.3 introduces various new tags and deprecated certain PHP functions which were being supported till ver 5.2. We recently upgraded the PHP version on one of our servers to 5.3 and found that it broke one site which was built on ver Joomla 1.0.x. Following is a simple update to ensure that the site still works on the new version of PHP 5.3.x.

Edit the Function.php file under  your Joomla directory on /public_html/includes/Cache/Lite.
Replace:
$arguments = func_get_args();

with

$arguments = func_get_args();
$numargs = func_num_args();
for($i=1; $i < $numargs; $i++){
$arguments[$i] = &$arguments[$i];
}

in includes/Cache/Lite/Function.php. It will fix compatibility view issues for joomla 1.0.x on php 5.3.x and this resolved the issue for us.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Create custom registration fields using Drupal

Thursday, May 6th, 2010

In Drupal websites, the login and registration is already integrated by default. But in one of our sites, our requirement is to add some custom fields in the drupal registration form. E.g., Full name, address, state, postcode, etc.

To fulfill our requirement we will use profile module. Once, the profile module is installed and enabled you can add custom fields from administrative menu/user management/profile as shown in figure below :

Custom Fields

Custom Fields

Firstly, you will have to choose the field type from the list like text field, multi text field, checkbox etc.

Secondly, give a category name to make a group of your custom fields.

E.g., If you are giving “category name”=Contact Information to the fields then those fields will become a group and show under Contact Information tab.

Finally, Give the title of the field and a machine readable name. Check this option “Visible in user registration form” from the form. If this option is not checked then this field will not show in the user registration form. If you want to validate the field then check the option ” The user must enter a value “.  Click Add field button to save the field.

In our example, we have added some custom fields like registration type, full name, firm, etc. as shown in the figure below.

Registration form

Custom Drupal Registration form

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Create a custom block in Drupal 6 using modules

Thursday, February 4th, 2010

In this post we will discuss on creating a custom block through a module in Drupal 6.  One question can come into your mind that we can create block from the administrative menu/site building/blocks. So, what is the need to create block from module.

The benefit of creating block from module is that, the module can be placed in any theme or in any other sites and once the module is enable the block will created automatically with its contents as we have configured in the module.

Some easy steps to create block through module :

Step 1 :

Create a folder (give a folder name e.g., mymodule) in the modules directory of your Drupal site.  The name which you are giving as a folder name will become your module name. To create a module there must have two files inside the module folder named as:

( a) mymodule.info (b) mymodule.module

mymodule.info file contains the general information of the module you created and mymodule.module file contains the main coding which creates the block.

# We are considering the module name as customblock and hence filenames are customblock.info and customblock.module

Step 2 :

Copy the code given below and paste it inside the customblock.info file.

; $Id: customblock.info,v 1.4 2009/02/18 22:02:46 dries Exp $
name = Custom Block
description = Creating Custom Block to demonstrate the block creation through module.
package = Diadem
version = VERSION

; Information added by drupal.org packaging script on 2008-10-08
version = “5.11″
project = “drupal”
datestamp = “1223496909″

This is the general information about the module and description field reflect the purpose of creating the module . Give the correct version which you are using for your drupal site. You need to change this information as per your requirement.

Step 3 :

Always remember that all the function name should start with the module name, e.g.  modulename_node_info(), modulename_perm(), etc. Copy the code given below and paste it inside the customblock.module file. You will observe that there has no php end tag at the end of the file. It is recommended in drupal that we donot need to end the php tag in the module pages.

<?php
/**
* Implementation of hook_node_info().
*/
function customblock_node_info() {
return array(
‘customblock’ => array(
‘name’ => t(‘Custom Block’),
‘module’ => ‘customblock’,
‘description’ => t(‘How to create a custom Block.’),
)
);
}

/**
* Access Permission of this module by hook_perm();
*/
function customblock_perm() {
return array(‘access customblock content’);
}

/**
* Implementation of hook_block().
*/
function customblock_block($op=’list’, $delta=0) {
// listing of blocks, such as on the admin/block page
if ($op == “list”) {
$block[0]["info"] = t(‘Custom Block’);
return $block;
}
else if($op == ‘view’)
{
$block_content=”;
$block_content.=’<div style=”border:1px solid #D6D6D6; background-color:#F2F2F2; padding: 5px;”>’;

$block_content.=’<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Why do we use it? <br />
<a href=”http://drupal.org/” target=”_blank”>Click Here</a> – Drupal.Org
</div>’;
$block_content.=’</div>’;

$block['subject'] = ‘Custom Block’;
$block['content'] = $block_content;
return $block;
}
}

Explanation :

1)  customblock_node_info() : This is the implementation of the hook_node_info function defined in Drupal. This function contains the information about the module. E.g.: name which will show in the module listing page. Description – Some general information about the module and module – module name.

2) customblock_perm(): This is the implementation of hook_perm function defined in Drupal.

3) customblock_block() : This is the implementation of hook_block function defined in Drupal. This function is responsible to return the block content. You can see, we have defined a variable $block_content which contain the block with its contents and properties.

$block['subject'] variable contains name of the block created. Finally, we assigned the $block_content into the $block['content'] variable beacuse this varible is responsible to create the block.

step 4 :

Enable the customblock module from administrative menu/site building/modules.

mod

Custom Block Module

Once you enable the module a block named as Custom Block will be created . Configure the block from administrative menu/site building/blocks. Finally, place the block where you want to display in your drupal site.

Block
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading ... Loading ...

How to create a view in Drupal

Sunday, June 21st, 2009

In this post we will discuss how to create view to display the listing  for the content types in drupal sites. In Drupal, you can create  dynamic pages from administrative menu > content management > content > Add Option. So, if you are creating some dynamic content E.g. pages where you should like to list the records for a particular set of data fields, then to fulfill this requirement you need to create a View which will display you the listing of the contents of any content types.

Here are some simple steps to create view for any content type. This example has been created using Drupal 5.10.

Step 1 :Basic View Setup

Download the view module (http://drupalmodules.com/module/views) and install the module. Once you enable the view module to can create view for all content types. From administrative menu/site building/views click on add option. Once, you click on the add option you will get a form as shown in figure below.

Basic information is the general information about the view which you will create.

viewinfo

Step 2 : Creating the View Display

You can display your created view in a page,block and both. Its up to to you how would you like to get the output.  For your help we are creating both the page and block. Please check the snapshots given below.

(a) Creating a page for the view.

Firstly, you have to check the provide page view option and give the URL of the view page. Use pager is the option for the pagination. If you don’t want pagination just unchecked the checkbox.

Page

b) Creating a Block for the view.

Check the provide block option to create a block for the view. Nodes per block indicated how many nodes you want to show in you block.

Block

Step 3 : Selecting the fields

Fields – Select the fields which you want to display for this content types. In our example, we are showing the node title and node body. You need to choose the field from the drop down and click on the add Field button. The selected field will display above as shown in figure.

Display Fields

Step 4 :View Arguments

Arguments – If you want to display a particular node then you need to configure this argument section otherwise leave it blank.  If you want to display a particular node then you select the argument from the drop down which you want to pass for the node and click add argument. The selected argument will show above as shown in figure.

Now, you need to set the wildcard value. Wildcard is the value which you pass from the URL. This will automatically fetch the value and show the corresponding node content.

For example, if you want to show node whose node ID is 11 then select node ID as the argument and set wildcard to %1.

%1 represent 11, the first argument you pass from the URL i.e., http://localhost/drupaldemo/?q=admin/build/views/page_entry/11

%2 represent 22, the second argument you pass from the URL i.e., http://localhost/drupaldemo/?q=admin/build/views/page_entry/11/22

Passing Argument

Step 5 :Creating Filters for the View

Filters – From this filter section you can filter the nodes which you want to show into your view page. In our example, we are filtering through node type and this is the simple and general way to filter. Select the Node: type from the dropdown and a click on the add filter button. Then select the page as the value of the node type.

Actually, in our example we are filtering the page type from all nodes through this filter option.

Filter

Step 6 : Set Your Sort Criteria

Sort Criteria – Select the field from the dropdown to sort. In our example, we are sorting as node ID i.e., latest node entry will show at the top of the listing. See the snapshot given below.

Sorting

Step 7 : Confirm the changes and preview the ‘View’

Finally, click on the save button to save all the provided information. Once, the view is save you can access the page by the URL you have provided. For our example, URL name is viewPage and to see the listing of the page nodes the url is http://sitename/?q=viewPage.

You will get the full url of your view page from  Administrative menu > site building > views > Listing page. You can also edit and delete this view from the section.

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)
Loading ... Loading ...

Two ways to add Joomla users using your custom code

Sunday, June 7th, 2009

The task of adding Joomla users, login/logout, activating/deactivating of users and password reset tasks etc are handled by the component com_users. In this post I will show how to add a joomla user from an external page.

Adding users externally can be done in two ways:
1. Create an html form with correct fields and submit it the correct Joomla url so that Joomla handles the registration, or
2. If you want to do it in your custom method, you need to create both the html form and the and the script that will add the user to the Joomla.

First, the regular way:

We create an html form with the correct field names and place the form in the joomla root. The field names should match with that of the general joomla registration form.

If we view the source of a Joomla registration form then we can see that the form has text fields named name, username, Email, password, password2 and hidden fields task, id, gid. There is another hidden field whose name is generated by JUtility::getToken() and the value of the field is 1. This field value is checked at the time of submitting the registration information. More about this in a moment. On submitting the registration form the registration information is processed by the register_save method of the UserController in com_user, which has been specified by the hidden field named task in the registration form.

Now we come to the UserController in the com_user. In the register_save method there is a line which checks the token value from the submitted form. As we are using a static html form we either need to generate the token name on the html form or if we don’t want to include the token in the form then we need to bypass the line of code which checks for the token.

JRequest::checkToken() or jexit( 'Invalid Token' );

In the register_save method of the UserController in the com_user. Since we will be using a html form commonly so we go for the second option and comment the line for checking token

// JRequest::checkToken() or jexit( 'Invalid Token' );

The submitted registration information is then processed by the register_save method. The data is actually saved in the save method of the UserController.

Now, the Other way:

Here we use the previously created form and submit it to the custom script that we create. This approach is basically to copy the functionality of the register_save() method in the UserController in com_user to an external script and submit the html registration form to that script.

Now onto the script:
To access the joomla environment in an external script, the following snippet is added to the start of the script:

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );
 
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
 
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

This snippet basically makes available the joomla framework available in the script. The code snippet is found in the index.php of the joomla root.

Now we import the functionality of the register_save() method in the script.

#1. Check for request forgeries, we comment this out since tokens are not generated in the html page

//JRequest::checkToken() or jexit( 'Invalid Token' );

#2. Get required system objects

$user 	      = clone(JFactory::getUser());
$pathway 	      = & $mainframe->getPathway();
$config	      = & JFactory::getConfig();
$authorize	      = & JFactory::getACL();
$document       = & JFactory::getDocument();

#3. If user registration is not allowed, show 403 not authorized(Not needed)

$usersConfig = &JComponentHelper::getParams( 'com_users' );
if ($usersConfig->get('allowUserRegistration') == '0')
	{
		JError::raiseError( 403, JText::_( 'Access Forbidden' ));
		return;
	}

#4. Initialize new usertype setting

$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype)
	{
		$newUsertype = 'Registered';
	}

#5. Bind the post array to the user object

if (!$user->bind( JRequest::get('post'), 'usertype' ))
	{
		JError::raiseError( 500, $user->getError());
	}

#6. Set some initial user values

$user->set('id', 0);
$user->set('usertype', '');
$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
 
$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());

#7. If user activation is turned on, we need to set the activation information(Not needed)

$useractivation = $usersConfig->get( 'useractivation' );
if ($useractivation == '1')
	{
		jimport('joomla.user.helper');
		$user->set('activation', md5( JUserHelper::genRandomPassword()) );
		$user->set('block', '1');
	}

#8. Save the details of the user

$user->save();

After this we can use the php header function to redirect the user to the desired location.

That’s it! I hope Joomla developers find this helpful and please feel to post your views and comments to this post.

1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 3.67 out of 5)
Loading ... Loading ...

Global variable to check Drupal user authentication

Thursday, April 23rd, 2009

In Drupal, the User is a core module of the CMS and is responsible for the user management section of Drupal websites. In this module a global variable is defined which returns the user Id (i.e, uid) of the currently logged user. All the users are stored in users table in the database and once we have the primary key of the table we can fetch all the details of that particular user who have logged in.

To get the user id (uid) of the currently logged user we will have to write this simple code :
<?php
global $user;
echo $user->uid;
?>

You can also access this variable from any modules or blocks throughout the site.
E.g., blog module, poll module, node module, block module etc.

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
Loading ... Loading ...