Drupal 7

Resize Images on the FLY !

4

Hi,
I was trying to make custom Media gallery page in Drupal with custom module
I made a content type that contain the image field with other fields i want Then i was trying to resize this images to fit my needs, I went through some modules but was useless.
till i found out how to resize images on the fly on drupal !
Follow This steps :-
Go to Admin => Configuration => Image styles
Then add Custom styles depending on what w=you want ( scale / resize …. ) The save.
And in you module function

$image_thumb = image_style_url('customstyle1', $item -> field_image['und']['0']['uri']);
$image_big_thumb = image_style_url('customstyle2', $item -> field_image['und']['0']['uri']);

Then you can use it in your html

Drupal 7

Custom Multi-Language 404 Page Drupal 7

0

Hi,
I wanted to make custom template for my 404 and even its changeable from Drupal admin so at any time you can change it
I have created content type and i called it 404
then i used preprocess+page hook to my theme template.php file

function mymoudle_preprocess_page(&$variables, $hook) {
    $language = $GLOBALS['language']->language;
 if(!drupal_is_front_page())
    {
        $status = drupal_get_http_header("status");
        if($status != '404 Not Found' && $variables['node']->type !='')
        {
            if($language!=$variables['node']->language || $variables['node']->type=='404_pages')
            {
                drupal_goto('page-not-found');
            }            
        }
 
    }
}
Apple-Mac-OS-X-Lion

Mac OS x Lion

4

Hello,

I have recently moved all my development environment to Mac OS x Lion ( MC 723 )

and i was looking for a lot of alternatives to my windows development stack and IDE ( PHP Designer )

I have tried Zend Server CE but i had a problem getting APC works because different arch x64 !

I have tried MAMP Pro but it sucks as well, i didn’t find it interesting

Finally i have tried using port ! and i really liked it ! and i found it really cool and worth trying ! with good support of almost all and last versions of extensions !

now i have my LAMP Stack UP with phpmyadmin as well !

then when i was looking for a good IDE, I have tried Zend Studio and i find it cool but its bit laggy with huge delay in building functions indexes from files !

I have tried Komodo IDE, it was better than Zend Studio but i didn’t find comfortable in some code completion and highlighting etc ..

Actually i was looking for Replacement for PHP Designer which i consider the most cool IDE in windows !

I have check other in IDE’s but forget all of THEM ! I found PHP Storm ! which beats all the IDE’s i have used till now !

Its Fast, Too much powerful, with very wide range of almost all feature you can ever NEED !!!

I like it and i’m using it in my development stack now !

For UML Diagrams and MySQL Schema tools i found mysql workbench works out of the box ! and i didn’t need for replacements

- The most thing that annoyed me is that I can’t create new files in MAC ! which is really stupid ! i’m using NewDoc now with customized package files to not show it in the Dockbar when launched

- Another things is the Finder !!! no Cut/Multi-Tabs/Show hidden files !!!!! it really miss alot of things !! I’m using Total Finder which i find all what i need in it.

- For SVN I’m using cornerstone which is really powerful and i like it.

I had some pain finding best things that fit my need ! Hope you find this things helpful when you start development in MAC !

Drupal 7

Get Drupal Nodes by Node Type

3

While building custom theme for Multilingual Drupal 7.x site that displaying custom news type at front page
i have wrote this function to get the nodes i want and display them in the front page

function mymodule_get_front_page_nodes()
{
    $lang = $GLOBALS['language']->language;
    if($lang =='ar')
    {
        $items = mymodule_get_node_ids('event','ar'); // type = event ( machine name of the content type )
    }else
    {
        $event_items = mymodule_get_node_ids('event','en'); // lang = en which is after translate the nodes
    }
 
function mymodule_get_node_ids($type,$lang) {
    $result = db_select('node','n')->fields('n',array('nid'))
        ->condition('type',$type,'=')
        ->condition('language',$lang,'=')
        ->condition('status','1','=')
        ->orderBy('created','DESC')
        ->execute();
    $node_list = array();
    foreach ($result as $nid) {
        $node_list[$nid->nid] = $nid->nid;
    }
    return node_load_multiple($node_list);
}
Drupal 7

Drupal Notifcations

0

Using drupal notification messages for some purposes ( form validations in custom modules )
it was really helpful to use the built in drupal notifications instead of manually writing ones !
Using menu hook in drupal to build customs menus.

function mymoudule_login()
{
    if ($_POST) {
        if (validate_login($_POST['username'], $_POST['password'])) {
            drupal_set_message('You have successfully logged in');
            drupal_goto('mymodule/success');
        } else {
            drupal_set_message('Wrong username or password');
        }
    }
    $messages = drupal_get_messages(); // notification message array()
    $html = $msg['status'][0]; // get the first message that we set
    $html .= '<form method="post">
<input name="username" type="text"><label>Username</label>
<input name ="password" type="password"><label>Password</label>
';
}
 
function validate_login($user, $pass)
{
    //put validation database here or whatever
}
Drupal 7

Managing Session Data in Drupal

2

I was so confused how to to manage the session data in Drupal when i was building some modules !

I looked at the manual and some other articles around and here is what i found

- Drupal session started in bootstrap so you don’t need to start session in you module !

- You can access session data normally via superglobal session array $_SESSION

- To save data in the Session array

ymodule_function_setname($name)
{
    $_SESSION['name'] = $name;
    drupal_save_session();
}
mymodule_function_getname()
{
    return $_SESSION['name'];
}

- There is some other Useful function for session management in Drupal

drupal_save_session	Determines whether to save session data of the current request.
drupal_session_commit	Commits the current session, if necessary.
drupal_session_destroy_uid	Ends a specific users session(s).
drupal_session_initialize	Initializes the session handler, starting a session if needed.
drupal_session_regenerate	Called when an anonymous user becomes authenticated or vice-versa.
drupal_session_start	Forcefully starts a session, preserving already set session data.
drupal_session_started	Returns whether a session has been started.
Drupal 7

Drupal Send Mail with SMTP !

1

Hello,

During building some modules for Drupal 7.x that needs to send an emails, and the mail() function is disabled its only allowed to send email via SMTP Server.

I have read drupal documentations and go through a lot of articles and posts but no hope !

I found plugin called SMTP for drupal and its working out of the box.

- Download and install the plugin.

and here is a function that show you how to send an email in drupal 7.x

function modulename_sendmail( $email = 'to_email@example.com')
{
$module = 'modulename';
$key = 'key'; // Random key
$language = language_default();
$lang = $GLOBALS['language']-&gt;language;
$params = array();
$from = 'noreplay@example.com';
$send = FALSE;
$message = drupal_mail($module, $key, $email, $language, $params, $from, $send);
 
$message['subject'] = 'Hello,This is Subject';
$message['body'] = array();
$message['body'][] = 'Greetings, Here your message body';
 
$system = drupal_mail_system($module, $key);
$message = $system-&gt;format($message);
$message['result'] = $system-&gt;mail($message);
}
Go to Top