You are here

node.inc in OM Tools 7

Body Classes Utility

File

tools/node/node.inc
View source
<?php

/**
 * @file
 * Body Classes Utility
 */

/**
 * OM Tools Node Utility Settings
 *
 */
function om_node(&$form) {
  drupal_add_js(drupal_get_path('module', 'om_tools') . '/tools/node/js/node.js', 'file');

  // Reset Node Form
  if (variable_get('om_tools_node_tools_reset', 0)) {
    foreach (node_type_get_types() as $type => $info) {
      variable_set('om_tools_node_' . $type . '_titles', 0);
    }
    variable_set('om_tools_node_body_classes', 1);
    variable_set('om_tools_node_body_classes_prefix', 'content-type-');
    variable_set('om_tools_node_terms_body_classes', 1);
    variable_set('om_tools_node_terms_body_classes_prefix', 'content-term-');
    variable_set('om_tools_node_tools_reset', 0);
  }

  // Node Form
  $form['node_tools'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node Tools'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t(''),
  );
  $form['node_tools']['node_tools_switch'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply node tools'),
    '#default_value' => variable_get('om_tools_node_tools_switch', 1),
    '#description' => t(''),
  );
  $form['node_tools']['node_settings'] = array(
    '#type' => 'fieldset',
    '#attributes' => array(
      'id' => 'om-group-node-tools-settings',
    ),
    '#title' => t('Body Classes Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t(''),
  );
  $form['node_tools']['node_settings']['node_body_classes'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add node body classes'),
    '#default_value' => variable_get('om_tools_node_body_classes', 1),
    '#description' => t(''),
  );
  $form['node_tools']['node_settings']['node_body_classes_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Node class prefix'),
    '#default_value' => variable_get('om_tools_node_body_classes_prefix', 'content-type-'),
    '#description' => t('content-type-page content-type-page-add | content-type-page-edit | content-type-page-view | content-type-page-delete section-node-add | section-node-edit | section-node-delete'),
  );
  $form['node_tools']['node_settings']['node_terms_body_classes'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add node terms body classes'),
    '#default_value' => variable_get('om_tools_node_terms_body_classes', 1),
    '#description' => t(''),
  );
  $form['node_tools']['node_settings']['node_terms_body_classes_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Term class prefix'),
    '#default_value' => variable_get('om_tools_node_terms_body_classes_prefix', 'content-term-'),
    '#description' => t('content-term-mobile content-term-technology content-term-educaton ...'),
  );
  $form['node_tools']['node_settings']['node_titles'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node Titles'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Hide all titles for each content type. If you are not using OM Base Theme add "body.om-tools-no-title h1.title { display: none; }" to your css.'),
  );
  foreach (node_type_get_types() as $type => $info) {
    $form['node_tools']['node_settings']['node_titles']['node_' . $type . '_titles'] = array(
      '#type' => 'checkbox',
      '#title' => t('Hide ' . $type . ' titles'),
      '#default_value' => variable_get('om_tools_node_' . $type . '_titles', 0),
      '#description' => t(''),
    );
  }
  $form['node_tools']['node_tools_reset'] = array(
    '#type' => 'checkbox',
    '#title' => t('Reset to default values.'),
    '#default_value' => variable_get('om_tools_node_tools_reset', 0),
    '#description' => t(''),
  );
  return $form;
}

/**
 * Override or insert om variables into the templates.
 */
function om_node_preprocess_html(&$vars) {
  if (variable_get('om_tools_node_tools_switch', 0)) {
    $classes = $vars['classes_array'];
    if (!$vars['is_front']) {

      // Add unique class for each page.
      $path = drupal_get_path_alias($_GET['q']);
      $classes[] = om_tools_class_safe('page-' . $path);

      // Add unique class for each website section.
      list($section) = explode('/', $path, 2);
      $arg = arg();
      if ($arg[0] == 'node') {
        $node_type = $vars['page']['#type'];
        if (variable_get('om_tools_node_' . $node_type . '_titles', 0)) {
          $classes[] = 'om-tools-no-title';
        }
        if (variable_get('om_tools_node_body_classes', 1)) {
          if ($arg[1] == 'add' && isset($arg[2])) {
            $section = 'node-add';

            //section-node-add
            $page_type = $arg[2];

            //-page
            $page_type_op = $arg[2] . '-add';

            //-page-add
          }
          elseif ($arg[1] == 'add' && !isset($arg[2])) {
            $section = 'node-add';

            //section-node-add
            $page_type = 'any';

            //-page
            $page_type_op = 'any' . '-add';

            //-page-add
          }
          elseif (is_numeric($arg[1]) && !isset($arg[2])) {
            $page_type = $node_type;

            //-page
            $page_type_op = $node_type . '-view';

            //-page-view
          }
          elseif (is_numeric($arg[1]) && $arg[2] != 'edit' && $arg[2] != 'delete') {
            $page_type = $node_type;

            //-page
            $page_type_op = $node_type . '-view';

            //-page-view
          }
          elseif (is_numeric($arg[1]) && ($arg[2] == 'edit' || $arg[2] == 'delete')) {
            $section = 'node-' . $arg[2];

            //section-node-edit || section-node-delete
            $page_type = $node_type;

            //-page
            $page_type_op = $node_type . '-' . $arg[2];

            //-page-edit or -page-delete
          }
          $classes[] = om_tools_class_safe(variable_get('om_tools_node_body_classes_prefix', 'content-type-') . $page_type);
          $classes[] = om_tools_class_safe(variable_get('om_tools_node_body_classes_prefix', 'content-type-') . $page_type_op);
        }

        // Term classes
        is_numeric($arg[1]) ? $terms = om_tools_taxonomy_node_get_terms(node_load($arg[1]), 'tid') : ($terms = '');
        if (!empty($terms) && variable_get('om_tools_node_terms_body_classes', 1)) {
          foreach ($terms as $term) {
            $classes[] = om_tools_class_safe(variable_get('om_tools_node_terms_body_classes_prefix', 'content-term-') . $term->name);
          }
        }

        //dsm($terms);
      }
      $classes[] = om_tools_class_safe('section-' . $section);
      $classes[] = 'om-tools-active';
    }
    $vars['classes_array'] = $classes;
    $vars['classes'] = implode(' ', $classes);

    // Concatenate with spaces.

    //dsm($vars);
  }
}

/**
 * Override or insert om variables into the templates.
 */
function om_node_preprocess_page(&$vars) {
  $vars['om_tools_title'] = drupal_get_title();
  if (variable_get('om_tools_node_tools_switch', 1)) {
    if (!$vars['is_front']) {
      if (arg(0) == 'node') {
        $node_type = $vars['page']['#type'];
        if (variable_get('om_tools_node_' . $node_type . '_titles', 0)) {
          $vars['om_tools_title'] = '';
        }
      }
    }

    //dsm($vars);
  }
}

// Get all node terms
function om_tools_taxonomy_node_get_terms($node, $key = 'tid') {
  static $terms;
  if (!isset($terms[$node->vid][$key])) {
    $query = db_select('taxonomy_index', 'r');
    $t_alias = $query
      ->join('taxonomy_term_data', 't', 'r.tid = t.tid');
    $v_alias = $query
      ->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
    $query
      ->fields($t_alias);
    $query
      ->condition("r.nid", $node->nid);
    $result = $query
      ->execute();
    $terms[$node->vid][$key] = array();
    foreach ($result as $term) {
      $terms[$node->vid][$key][$term->{$key}] = $term;
    }
  }
  return $terms[$node->vid][$key];
}

Functions

Namesort descending Description
om_node OM Tools Node Utility Settings
om_node_preprocess_html Override or insert om variables into the templates.
om_node_preprocess_page Override or insert om variables into the templates.
om_tools_taxonomy_node_get_terms