home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (ui.theme.inc)

This example Drupal source code file (ui.theme.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, attributes, check_plain, div, drupal_render, element, foreach, form, function, output, php, row, table, variables

The ui.theme.inc Drupal example source code

<?php
// $Id: ui.theme.inc,v 1.1.2.2 2011/02/09 17:38:00 fago Exp $

/**
 * @file Rules theme functions
 */


/**
 * Themes a tree of rule elements in a draggable table.
 *
 * @ingroup themeable
 */
function theme_rules_elements($variables) {
  $form = $variables['element'];
  $form['#theme'] = 'table';
  $form['#header'] = array(t('Elements'), t('Weight'), t('Operations'));
  $form['#attributes']['id'] = 'rules-' . drupal_html_id($form['#caption']) . '-id';

  foreach (element_children($form) as $element_id) {
    $element = &$form[$element_id];

    // Add special classes to be used for tabledrag.js.
    $element['parent_id']['#attributes']['class'] = array('rules-parent-id');
    $element['element_id']['#attributes']['class'] = array('rules-element-id');
    $element['weight']['#attributes']['class'] = array('rules-element-weight');

    $row = array();
    $row[] = theme('indentation', array('size' => $element['#depth'])) . drupal_render($element['label']);

    $row[] = drupal_render($element['weight']) . drupal_render($element['parent_id']) . drupal_render($element['element_id']);
    $row[] = array('class' => 'rules-operations', 'data' => $element['operations']);

    $row = array('data' => $row) + $element['#attributes'];
    $row['class'][] = 'draggable';
    if (!$element['#container']) {
      $row['class'][] = 'tabledrag-leaf';
    }
    $form['#rows'][] = $row;
  }
  if (!empty($form['#rows'])) {
    drupal_add_tabledrag($form['#attributes']['id'], 'match', 'parent', 'rules-parent-id', 'rules-parent-id', 'rules-element-id', TRUE, 10);
    drupal_add_tabledrag($form['#attributes']['id'], 'order', 'sibling', 'rules-element-weight');
  }
  else {
    $form['#rows'][] = array(array('data' => t('None'), 'colspan' => 3));
  }
  if (!empty($form['#add'])) {
    $row = array();
    $row[] = array('data' => $form['#add'], 'colspan' => 3);
    $form['#rows'][] = array('data' => $row, 'class' => array('rules-elements-add'));
  }

  // Add a wrapping div.
  return '<div class="rules-elements-table">' . drupal_render($form) . '</div>';
}

/**
 * Themes the rules form for editing the used variables.
 *
 * @see RulesPluginUI::getVariableForm()
 * @ingroup themeable
 */
function theme_rules_ui_variable_form($variables) {
  $elements = $variables['element'];

  $table['#theme'] = 'table';
  $table['#header'] = array(t('Data type'), t('Label'), t('Machine name'), array('data' => t('Weight'), 'class' => array('tabledrag-hide')));
  $table['#attributes']['id'] = 'rules-' . drupal_html_id($elements['#title']) . '-id';

  foreach (element_children($elements['items']) as $key) {
    $element = &$elements['items'][$key];
    // Add special classes to be used for tabledrag.js.
    $element['weight']['#attributes']['class'] = array('rules-element-weight');

    $row = array();
    $row[] = array('data' => $element['type']);
    $row[] = array('data' => $element['label']);
    $row[] = array('data' => $element['name']);
    $row[] = array('data' => $element['weight']);
    $row = array('data' => $row) + $element['#attributes'];
    $row['class'][] = 'draggable';
    $table['#rows'][] = $row;
  }
  $elements['items']['#printed'] = TRUE;
  if (!empty($table['#rows'])) {
    drupal_add_tabledrag($table['#attributes']['id'], 'order', 'sibling', 'rules-element-weight');
  }

  // Theme it like a form item, but with the description above the content.
  $attributes['class'][] = 'form-item';
  $attributes['class'][] = 'rules-variables-form';

  $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  $output .= theme('form_element_label', $variables);
  if (!empty($elements['#description'])) {
    $output .= ' <div class="description">' . $elements['#description'] . "</div>\n";
  }
  $output .= ' ' . drupal_render($table) . "\n";
  // Add in any further children elements.
  foreach (element_children($elements, TRUE) as $key) {
    $output .= drupal_render($elements[$key]);
  }
  $output .= "</div>\n";
  return $output;
}

/**
 * Themes a view of multiple configuration items.
 * @ingroup themeable
 */
function theme_rules_content_group($variables) {
  $element = $variables['element'];
  $output = array();
  foreach (element_children($element) as $key) {
    $output[] = drupal_render($element[$key]);
  }
  $output = array_filter($output);
  $heading = !empty($element['#caption']) ? "<span class='rules-content-heading'>" . $element['#caption'] . ': </span>' : '';
  if (!empty($output)) {
    return '<div class="rules-element-content-group">' . $heading . implode(', ', $output) . '</div>';
  }
}

/**
 * Themes the view of a single parameter configuration.
 * @ingroup themeable
 */
function theme_rules_parameter_configuration($variables) {
  $element = $variables['element'];
  $content = drupal_render_children($element);
  // Add the full content to the span's title, but don't use drupal_attributes
  // for that as this would invoke check_plain() again.
  $title = strip_tags($content);
  $element['#attributes']['class'][] = 'rules-parameter-configuration';
  $attributes = drupal_attributes($element['#attributes']) . " title='$title'";

  $label_attributes['class'][] = 'rules-parameter-label';
  if (!empty($element['#info']['description'])) {
    $label_attributes['title'] = $element['#info']['description'];
  }
  $label_attributes = drupal_attributes($label_attributes);

  $output = "<span $label_attributes>" . check_plain($element['#info']['label']) . ': </span>';
  $output .= "<span $attributes>" . truncate_utf8($content, 30, TRUE, TRUE) . "</span>";
  return $output;
}

/**
 * Themes info about variables.
 * @ingroup themeable
 */
function theme_rules_provides_variable_view($variables) {
  $element = $variables['element'];

  $label_attributes['class'][] = 'rules-variable-label';
  if (!empty($element['#info']['description'])) {
    $label_attributes['title'] = $element['#info']['description'];
  }
  $label_attributes = drupal_attributes($label_attributes);

  $output = check_plain($element['#info']['label']);
  $output .= ' (' . check_plain($element['#info']['type']) . ')';
  return "<span $label_attributes>" . $output . '</span>';
}

/**
 * Themes help for using the data selector.
 * @ingroup themeable
 */
function theme_rules_data_selector_help($variables) {
  $variables_info = $variables['variables'];
  $param_info = $variables['parameter'];

  $render = array(
    '#type' => 'fieldset',
    '#title' => t('Data selectors'),
    '#pre_render' => array(),
    '#attributes' => array(),
  );
  // Make it manually collapsible as we cannot use #collapsible without the
  // FAPI element processor.
  $render['#attached']['js'][] = 'misc/collapse.js';
  $render['#attributes']['class'][] = 'collapsible';
  $render['#attributes']['class'][] = 'collapsed';

  $render['table'] = array(
    '#theme' => 'table',
    '#header' => array(t('Selector'), t('Label'), t('Description')),
  );
  foreach (RulesData::matchingDataSelector($variables_info, $param_info) as $selector => $info) {
    $info += array('label' => '', 'description' => '');
    $render['table']['#rows'][] = array(check_plain($selector), check_plain(drupal_ucfirst($info['label'])), check_plain($info['description']));
  }
  return drupal_render($render);
}

/**
 * Themes the rules log debug output.
 * @ingroup themeable
 */
function theme_rules_log($variables) {
  $element = $variables['element'];
  drupal_add_css(drupal_get_path('module', 'rules') . '/ui/rules.ui.css');

  $output = '<div class="rules-debug-log">';
  $output .= '<h3>' . t('Rules evaluation log') . '</h3>';
  $output .= $element['#children'];
  $output .= '</div>';
  return $output;
}

/**
 * Themes rules autocomplete forms.
 * @ingroup themeable
 */
function theme_rules_autocomplete($variables) {
  $element = $variables['element'];
  drupal_add_js(drupal_get_path('module', 'rules') . '/ui/rules.autocomplete.js');
  drupal_add_library('system', 'ui.autocomplete');
  drupal_add_library('system', 'ui.button');

  $element['#attributes']['type'] = 'text';
  element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
  _form_set_class($element, array('form-text', 'rules-autocomplete'));

  $id = $element['#attributes']['id'];
  $id_button = drupal_html_id($id . '-button');

  $js_vars['rules_autocomplete'][$id] = array(
    'inputId' => $id,
    'source' => url($element['#autocomplete_path'], array('absolute' => TRUE)),
  );
  drupal_add_js($js_vars, 'setting');

  $output = '<div class="rules-autocomplete">';
  $output .= '<input' . drupal_attributes($element['#attributes']) . ' />';
  $output .= '</div>';

  return $output;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal ui.theme.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.