Tag Archive for drupal

Drupal example on how to use form alter hook

function mysite_form_alter(&$form, &$form_state, $form_id) 
{
  switch ($form_id) 
  {
    case 'user_profile_form':
      $form['account']['name']['#size'] = 30;
      $form['account']['mail']['#size'] = 30;
      break;
  }
}

source

Drupal 6 CCK Get Allowed Fields List

function get_cck_text_field_allowed_values($field_name)
{
  $result = db_query("SELECT * FROM {node_field} WHERE field_name = '%s'", $field_name);
  if($row = db_fetch_array($result)){
    $field = unserialize($row['global_settings']);
    if($field['allowed_values']){
      
      $allowed_values = array();
      if ($field['allowed_values_php']) {
        ob_start();
        $result = eval($field['allowed_values_php']);
        if (is_array($result)) {
          $allowed_values = $result;
        }
        ob_end_clean();
      }
      if (!$allowed_values) {

        $list = explode("
", $field['allowed_values']);
        $list = array_map('trim', $list);
        $list = array_filter($list, 'strlen');
        foreach ($list as $opt) {
          // Sanitize the user input with a permissive filter.
          $opt = content_filter_xss($opt);
          list($key, $value) = explode('|', $opt);
          $allowed_values[$key] = (isset($value) && $value !=='') ? $value : $key;
        }
      }
      return $allowed_values;
    }
  }
  return NULL;
}

source

Drupal 6 custom logged in block

<?php  global $user; ?>

<div id="portal-login">
  <p><?php print l($user->name,'user/'.$user->uid); ?>, Sie sind angemeldet.<br />
    <b><?php print l("logout","logout"); ?></b></p>
</div>

source

Theme Primary Links Drupal 6.x Adding spans and useful ID’s

/**
 * Adds spans and useful ID's to the primary links.
 *
 */
function THEMENAME_links($links, $attributes = array('class' => 'links')) {
  $output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      
      $class = '';
      $needle = array('&', ' ');
      $replace = array('and', '-');
      $id = strtolower(str_replace($needle, $replace, $link['title']));
      			
      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
        $class .= ' active';
      }
      $output .= '<li id="'. $id .'"';
      
      if ($class) {
      	$output .= ' class="'. $class .'"';
      }
      
      $output .= '>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $link['title'] = '<span>'.$link['title'].'</span>';
        $link['html'] = TRUE;
        $output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li>
";
    }

    $output .= '</ul>';
  }

  return $output;
}

source

Drupal – Get the image path of a CCK field from the fid

$file = field_file_load($node->website_logo_fid);
$output .= "<p><img src="".$file['filepath']."" alt="" /></p>";

source

Target=_blank in Drupal link

 l($node->title, 'node/'. $node->nid, array('target' => '_blank'))

source

Add Drupal page template suggestions based on path alias.

/**
 * Override or insert variables into the page templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered.
 */
function genesis_custom_preprocess_page(&$vars, $hook) {  
   // Add page template suggestions based on path alias.
   $alias = drupal_get_path_alias($_GET['q']);
   if ($alias != $_GET['q']) {
     $template_filename = 'page';
     foreach (explode('/', $alias) as $path_part) {
       $template_filename = $template_filename . '-' . $path_part;
       $vars['template_files'][] = $template_filename;
     }
   }
}

source

Drupal Views AJAX Reset Button for Exposed Filters in

function CUSTOM_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
        $form['reset'] = array(
           '#type' => 'markup',
           '#value' => '<input '. drupal_attributes(array('type' => 'button', 'value' => t('Reset') )) .'onclick="javascript:$(this.form).clearForm();$(this.form).submit();" class="form-submit" />',
    );
    }
}

source

Create Taxonomies Programmaticallly

function updateCategory($vid, $term) {

      // Make sure the database has all the entries
      $parent = 0;
	
      // Is there an ID for this term already?
      $tid = db_result(db_query("SELECT tid FROM {term_data} WHERE vid=%d AND name='%s'",$vid,$term));
      if(!$tid) {
      // Create a new row in term_data for this term
      $tid = db_next_id('{term_data}_tid');
      db_query("INSERT INTO {term_data} VALUES(%d, 1, '%s', '', 0)", $tid, $term);
      db_query("INSERT INTO {term_hierarchy} VALUES(%d, %d)", $tid, $parent);
      }
      // The next term will be a child of this one
      $parent = $tid;
    
      // Now simply associate $tid with the product
      db_query('INSERT INTO {term_node} VALUES(%d, %d)', $product->nid, $tid);
      }

source

Drupal 6.x Find PHP code in database

select nid, vid from node_revisions where format in (select format from filters where module = 'php' and delta = 0);

select cid, nid from comments where format in (select format from filters where module = 'php' and delta = 0);

select bid, info from boxes where format in (select format from filters where module = 'php' and delta = 0);

source