nortmas
5/11/2014 - 2:00 PM

Hook theme

Hook theme

function [my_module]_theme($existing, $type, $theme, $path) {
  return array(
    '[my_module]_template' => array(
      'variables' => array('var_name' => NULL),
      'template' => drupal_get_path('theme', 'theme_name') . '/templates/[my-module]-template' // theme file
    ),
  );
}

theme('mymodule_template', array('var_name' => mymodule_variable()));


/**********************************************************************************************************************************/

function hook_menu() {
  $items['test'] = array(
    'page callback' => 'modulename_test',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function hook_theme($existing, $type, $theme, $path) {
  return array(
    'test_template' => array(
      'variables' => array('test' => NULL),
      'template' => '/templates/test'
    ),
  );
}

function modulename_test() {
  $page = array(
    '#theme' => 'test_template',
    '#test' => 123,
  );
  return render($page);
}

/**********************************************************************************************************************************/

/**
 * Implements hook_theme().
 */
function m60_theme($existing, $type, $theme, $path) {
// Добавляет темизацию через темплейты для всех существующих кастомных файлов темизации форм
  $hooks['m60_form'] = array(
    'render element' => 'form',
    'path' => path_to_theme() . '/templates/forms',
    'template' => 'm60-form',
  );
  $hooks += drupal_find_theme_templates($hooks, '.tpl.php', $path);
  return $hooks;
}

/**
 * Implements hoot_form_alter().
 *
 * Set custom meta theme for forms.
 */
function m60_form_alter(&$form, $form_state, $form_key) {
  $ignore = variable_get('m60_form_alter_ignore', array());
  if (!in_array($form_key, $ignore)) {
    if (!is_array($form['#theme'])) {
      $form['#theme'] = array($form['#theme']);
    }
    $form['#theme'][] = 'm60_form';
  }
}

/**
 * Preprocess form, threat forms like entities.
 */
function m60_preprocess_m60_form(&$variables) {
  
  $preprocess_entity_loaded = &drupal_static('m60_preprocess_loaded', FALSE);
  
  $form = &$variables['form'];
  
  if (!$preprocess_entity_loaded) {
    $preprocess_entity_loaded = TRUE;
    $file = path_to_theme() . '/m60.preprocess.inc';
    if (is_file($file)) {
      require_once $file;
    }
  }
  $preprocess = array();
  $variables['theme_hook_suggestions'][] = 'm60_form__' . $form['#form_id'];
  $preprocess[] = 'm60_preprocess_form__' . $form['#form_id'];
  
  foreach ($preprocess as $preproces) {
    if (function_exists($preproces)) {
      $preproces($variables);
    }
  }
}

/**********************************************************************************************************************************/