vendredi 6 juin 2014

[Drupal] Activate clean urls with 1and1 host

If you can not activate clean urls, and your site is hosted in 1and1 servers, you have probably to modify the .htaccess file in your site root folder, to the following :

# Various rewrite rules.

  RewriteEngine on

  # Modify the RewriteBase if you are using Drupal in a subdirectory and
  # the rewrite rules are not working properly.
  RewriteBase /sitename # PUT YOUR SITE NAME (FOLDER NAME) HERE

  # Rewrite old-style URLs of the form 'node.php?id=x'.
  #RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteCond %{QUERY_STRING} ^id=([^&]+)$
  #RewriteRule node.php index.php?q=node/view/%1 [L]

  # Rewrite old-style URLs of the form 'module.php?mod=x'.
  #RewriteCond %{REQUEST_FILENAME} !-f
  #RewriteCond %{REQUEST_FILENAME} !-d
  #RewriteCond %{QUERY_STRING} ^mod=([^&]+)$
  #RewriteRule module.php index.php?q=%1 [L]

  # Rewrite current-style URLs of the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


# $Id: .htaccess,v 1.66 2005/03/20 19:15:00 dries Exp $
Good luck!!

jeudi 5 juin 2014

[Drupal] Top 3 administration modules

Administration menu

To have a cool dropdown top menu, instead of navigating in admin pages till target page ...



Module Filter

Show modules in groups, and show a switch control for each module, to activate/desactivate it.



Coffee

Reach faster the admin page, by typing words describing it. Inspired by Mac apps Alfred and Spotlight.



Good drupal administration !

jeudi 26 décembre 2013

[Drupal7] drupal_form_submit vs node_save

When I want to create or update a node programmatically I often use node_save core function. Example :

global $user;
$customNode = new stdClass();
$customNode->type = 'page';
node_object_prepare($customNode);
$customNode->uid = $user->uid;
$customNode->name = $user->name;
$customNode->title = 'Your page title';
$customNode->language = 'en';
$customNode->path['alias'] = 'Your Alias';
$customNode->comment = 1;
$customNode->status = 1;        // 1 means published
$customNode->promote = 0;
$customNode->revision = 0;
$customNode->changed = $_SERVER['REQUEST_TIME'];
$customNode->created = $_SERVER['REQUEST_TIME'];
// FIELDS
$customNode->field_1[LANGUAGE_NONE][0]['value'] = $entity_id;
$customNode->field_2[LANGUAGE_NONE][0]['value'] = $entity_parent_id;
// SAVE 
node_submit($customNode);
node_save($customNode);

This method is probably the most elegant, but in some treatment cases you can't use it. For example when you want to fill dynamically some fields, without enumarating their names.

Indeed I had some xml files containing node structures and generic information fields (parametrized product fields), and I wanted to import these xml files into drupal nodes.

I looked for an other way to do it, and I found the drupal_form_submit function way. Example to use it :

global $user;
module_load_include('inc', 'node', 'node.pages');
//////////////////////////////////////////////////
// 2 OPTIONS :
// OPTION 1 : TO CREATE NEW NODE 
$node = (object) array(
 'uid' => $user->uid,
 'type' => $content_type,
 'language' => LANGUAGE_NONE,
);
// OPTION 2 : TO UPDATE EXISTING NODE
// $node = node_load($nid);
//////////////////////////////////////////////////
node_object_prepare($node);

// Prepare form_state for the node save
$form_state = array();
$form_state['node'] = $node;

// extract from xml
$entity_name = (string)$xml->name;
$entity_id = (integer)$xml->id;

// Static fields
$form_state['values']['title'] = $entity_name;
$form_state['values']['field_entity_id'][LANGUAGE_NONE][0]['value'] = $entity_id;
 
// Dynamic info fields
$fields_xml_values = get_info_fields_from_xml($instructionData); // Array( field_name => value )
foreach($content_type_fields as $field_name => $value) {
    $field_xml_value = $fields_xml_values[$field_name];
    $form_state['values'][$field_name]['und'][0]['value'] = $field_xml_value;
}

// Without this line, NOT QUITE SURE WHY, it wont work.
$form_state['values']['op'] = t('Save');

// Save the node
drupal_form_submit('MY_CONTENT_TYPE_node_form', $form_state, (object)$node);
 
// Tell watchdog if any of the fields fail validation.
$errors = form_get_errors();
if (!empty($errors)) {
    foreach ($errors as $field_name => $message) {
 watchdog('xml import', '%field: %message', array('%message'=> $message, '%field' => $field_name));
    }
}

The last method broughta more flexibility, so I thought it would be usefull for you too!

mardi 26 novembre 2013

[Drupal] DruWiki : simple wiki organising projects and guides

I made a simple wiki to help companies (web agencies for example) list their projects, create guides related to these projects, and create links to project demonstrations (by categories).

You can have a demonstration of druwiki here : http://demos.jalalbricha.com/druwiki

To download or clone it, here : https://github.com/jbricha/druwiki

Maybe I will create a real Drupal distribution by following steps in here : https://drupal.org/developing/distributions/drupalorg , but I will need some hours to do it (because I have never did it before), and I'm not sure it's adding initial content, blocs positions and views ...

If you can help me do that (with features module maybe ...) that would very great!

Good luck apluche!

lundi 25 novembre 2013

[jQuery] jimgPreview : Advanded image preview jQuery plugin

When I needed to display simple image previews when hovering links to images, I found the jQuery imgPreview here : http://james.padolsey.com/demos/imgPreview/full/ .

The problem with this plugin is when the link is located in the page bottom, a main part of the image preview (or all of it) is not diplayed at the screen. Because the image preview isalways displayed under the link position.

To resolve this problem, I made an other simple jQuery plugin that adapt the preview position according to the link position relatively to page bottom.

You can download it from my github : https://github.com/jbricha/jimgPreview

Try a demonstration

You are not forced to use href attribute, you can use any attribute you want, and init jimgPreview like that :
  $(document).ready(function() {
   $('a.jimg').jimgPreview({
     srcAttr: 'preview_href'
   } );
  });

mercredi 20 novembre 2013

[Drupal] Sort according to field node title

Same as term references, node reference fields are sorted occording to the nid, and not the node name.
What we should do to sort by nid :
Since the course field is an entity reference the sort would be working on the node id and not the node name. can you check this.
  1. Go to advanced, add a relationship to the content type ( displayed as Content; My content type (field_my_content_type) )
  2. Go back to that views field configuration (Or create sort criteria Content: Title) and select the relationship from the drop down. 
  3. Go the the format settings and make the new field sortable.
It's tactful for a simple title field sort :/ But good luck !!

mardi 19 novembre 2013

[Drupal] Sort views according to a field term name alphabetical order

If you try to sort a view according to a vocabulary field, it will be sorted by terms tids and not by terms names. Views only sees the data as a bunch of tids. Because of this, allowing the sorting on that column would produce some unintended results.

Instead, you need to create a relationship. Once you've created your Content Taxonomy field, go to your View and add a relationship. Under the "Content" selections, pick the Content Taxonomy field you want sortable. Save that, and now you need to create a new field. Select "Taxonomy: Term" as your field and continue. In the new options and in the "Relationship" dropdown, select the relationship that you just created, make any other changes you'd like, then click the "Update" button. At this point, you should be able to click your "Style: Table" gear and be able to sort by term name. Just remember to save the changes to the View itself! =)