Category Archives: Web Developing

Render a Views in Drupal 6

Code copied from og module:

  $closerview = views_get_view('viewscloser');
  $closerview->is_cacheable = FALSE;
  $closerview->set_display('default');
  // TODOL: not working
  $closerview->override_path = "node/$node->nid";
  $closerview->set_arguments(array($node->nid));  
  $closerview->display_handler->set_option('style_options', array('description' => $node->og_description));
  print $closerview->render();

Custom Views exposed form for Drupal

Take a look at views.module, add a line you will be able to get all the themes


$form['#theme'] = views_theme_functions('views_exposed_form', $view, $display);
drupal_set_message('


A small drupal MySQL snippet


select uid, (select value from profile_values where fid=24 and uid=pv.uid) as c_bpartner_id, value from profile_values pv where uid in (select uid from profile_values where fid=24) and fid=17 order by c_bpartner_id


Disable subject field in Drupal comment

modulename.install

<?php

function modulename_update_6203(){

    $items = array();

    $types = node_get_types('types', NULL, TRUE);

    foreach($types as $type) {        

        variable_set('comment_subject_field_'. $type->type, 0);

    }

    return $items;

}


Extract HTML from PHP

Do not user regular expression, use DOMDocument instead

Function provided:

<?php

function parseHTMLURL($url = 'http://www.google.com') {    

    $content = file_get_contents($url);

    

    // removing some text for ignoring error

    $content = str_replace('id="special-offer-block"', "", $content);

    $dom = new DOMDocument();

    $html = $dom->loadHTML($content);

    

    $node = $dom->getElementById("node");

    

    // Go to level-2 div

    $divs = $node->getElementsByTagName("div");    

    foreach($divs as $k => $d) {        

        $node = $d;

        break;

    }

    

    $divs = $node->getElementsByTagName("div");    

    foreach($divs as $k => $d) {        

        $node = $d;

        break;

    }

        

    // get DomElement innerHTML

    $elem = $d;

    $innerHTML = ''; 

    $children = $elem->childNodes;

    foreach ($children as $child) {

            $tmp_doc = new DOMDocument();

            $tmp_doc->appendChild($tmp_doc->importNode($child,true));       

            $out = $tmp_doc->saveHTML();            

            $innerHTML .= $out;

    }         

    return $innterHTML;

}

*: Install php-xml for this extension


Embed views in custom module

1. Embed code to module

<?php

/**

 * Implementation of hook_views_api.

 */

function modulename_views_views_api() {

  return array(

    'api' => 2,

    'path' => drupal_get_path('module', 'modulename'),

  );

}

2. Embed exported views to modulename.views_default.inc

<?php

/**

 * Implementation of hook_views_default_views().

 */

function modulename_views_default_views() {

  /*Exported views code paste here*/

 

  $views[$view->name] = $view;

  return $views;

}

+ More things: Overriding views from other module

1. Define override views code

<?php

/**

* Implementation of hook_views_default_views_alter().

*/

function modulename_views_default_views_alter(&$views) {   

  module_load_include('inc', 'modulename', 'modulename.views_default_override');

  $new_views = modulename_views_default_override_views();

  foreach($new_views as $key => $view) {

    if(array_key_exists($key, $views)) {

      if(is_object($view)){        

        $views[$key] = $view;

      }

    }

  }

}

2. Embed exported changed views to modulename.views_default_override.inc

<?php

 

function modulename_views_default_override_views() {

  /*Exported views code paste here*/

  

  $views[$view->name] = $view;

  return $views;

}


Update module to highest weight in Drupal

Define below code in modulename.install

Run update.php

<?php

 

function modulename_update_6201() {

  $items = array();

  $max_weight = db_result(db_query('SELECT MAX(weight) FROM {system}'));

  $items[] = update_sql('UPDATE {system} SET weight = ' . ($max_weight+1) . ' WHERE name = "modulename" AND type="module"');

  return $items;

}