You are here

function headerimage_form_alter in Header image 7

Same name and namespace in other branches
  1. 5 headerimage.module \headerimage_form_alter()
  2. 6 headerimage.module \headerimage_form_alter()

Implementation of hook_form_alter

Add display conditions to header image node forms

File

./headerimage.module, line 465
headerimage.module Conditionally display an node in a block.

Code

function headerimage_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, '_node_form') && isset($form['type']['#value']) && in_array($form['type']['#value'], variable_get('headerimage_node_type', array()), true) && user_access('maintain display conditions')) {
    $form['headerimage'] = array(
      '#type' => 'fieldset',
      '#title' => t('Display conditions'),
      '#description' => t('This node is displayed in a Header Image block when one of the conditions below are evaluated true.'),
      '#collapsible' => true,
      '#collapsed' => false,
      '#group' => 'additional_settings',
      '#weight' => 0,
    );
    $form['headerimage']['headerimage_block'] = array(
      '#type' => 'select',
      '#title' => t('Block name'),
      '#description' => t('The block in which this node is displayed.'),
      '#options' => headerimage_get_blocks(),
      '#default_value' => isset($form['#node']->headerimage_block) ? $form['#node']->headerimage_block : '',
      '#required' => true,
      '#multiple' => false,
    );
    $form['headerimage']['headerimage_weight'] = array(
      '#type' => 'weight',
      '#title' => t('Condition weight'),
      '#description' => t('Determines the order of in which the nodes are evaluated. The conditions of a node with a smaller weight will be evaluated first, those with a larger weight are evaluated later. A default image (the one displayed if the conditions of all other images fail) should have the largest weight: 10.'),
      '#default_value' => isset($form['#node']->headerimage_weight) ? $form['#node']->headerimage_weight : '',
      '#delta' => 10,
    );
    $condition_types = variable_get('headerimage_condition_types', array(
      'nid' => 'nid',
    ));
    if (!empty($condition_types)) {
      foreach ($condition_types as $type) {
        if ($type != '0') {
          $all_types = headerimage_get_condition_types();
          $title = t("@name condition", array(
            '@name' => $all_types[$type],
          ));
          $name = 'headerimage_condition_' . $type;
          switch ($type) {
            case 'nid':
              $form['headerimage'][$name] = array(
                '#type' => 'textarea',
                '#title' => $title,
                '#description' => t("Enter node id's separated by comma's."),
                '#default_value' => isset($form['#node']->{$name}) ? $form['#node']->{$name} : '',
                '#wysiwyg' => false,
                '#rows' => 4,
              );
              break;
            case 'url':
              $form['headerimage'][$name] = array(
                '#type' => 'textarea',
                '#title' => $title,
                '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. %front is the front page.", array(
                  '%front' => '<front>',
                )),
                '#default_value' => isset($form['#node']->{$name}) ? $form['#node']->{$name} : '',
                '#wysiwyg' => false,
                '#rows' => 4,
              );
              break;
            case 'taxonomy':
              $options = array();
              if (module_exists('taxonomy')) {
                $vocab = taxonomy_get_vocabularies();
                if (!empty($vocab)) {
                  foreach ($vocab as $v) {
                    $taxonomy = taxonomy_get_tree($v->vid);
                    if (!empty($taxonomy)) {
                      foreach ($taxonomy as $tag) {
                        $options[$tag->tid] = check_plain($tag->name);
                      }
                    }
                  }
                }
                if (!empty($options)) {
                  $description = t("One tag or multiple tags can be selected.");
                }
                else {
                  drupal_set_message(t("No vocabulary or tags defined. Please create vocabulary and tags or remove taxonomy from the !settings.", array(
                    '!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
                  )));
                  break;
                }
                $form['headerimage'][$name] = array(
                  '#type' => 'select',
                  '#title' => $title,
                  '#description' => $description,
                  '#default_value' => isset($form['#node']->{$name}) ? $form['#node']->{$name} : '',
                  '#options' => $options,
                  '#multiple' => true,
                );
              }
              else {
                drupal_set_message(t("The taxonomy module is not enabled. Please enable the module or remove it from the !settings.", array(
                  '!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
                )));
              }
              break;
            case 'book':
              if (module_exists('book')) {
                $options = array();
                $books = book_get_books();
                if (count($books)) {
                  $description = t("One book or multiple books can be selected.");
                  foreach ($books as $key => $book) {
                    $options[$key] = $book['title'];
                  }
                }
                else {
                  $description = t("No books defined. Please create a book before using it as a condition.");
                }
                $form['headerimage'][$name] = array(
                  '#type' => 'select',
                  '#title' => $title,
                  '#description' => $description,
                  '#default_value' => isset($form['#node']->{$name}) ? $form['#node']->{$name} : '',
                  '#options' => $options,
                  '#multiple' => true,
                );
              }
              else {
                drupal_set_message(t("The book module is not enabled. Please enable the module or remove it from the !settings.", array(
                  '!settings' => l(t('Header Image settings'), 'admin/settings/headerimage/settings'),
                )));
              }
              break;
            case 'nodetype':
              $nodes = node_type_get_types();
              foreach ($nodes as $node) {
                $nodetype[$node->type] = check_plain($node->name);
              }
              $form['headerimage'][$name] = array(
                '#type' => 'select',
                '#title' => $title,
                '#description' => t('Select one or multiple content types'),
                '#default_value' => isset($form['#node']->{$name}) ? $form['#node']->{$name} : '',
                '#options' => $nodetype,
                '#multiple' => true,
              );
              break;
            case 'php':
              $form['headerimage'][$name] = array(
                '#type' => 'textarea',
                '#title' => $title,
                '#description' => t("Enter PHP code between &lt;?php ?&gt; tags. Note that executing incorrect PHP-code can break your Drupal site."),
                '#default_value' => isset($form['#node']->{$name}) ? $form['#node']->{$name} : '',
                '#wysiwyg' => false,
                '#rows' => 4,
              );
              break;
          }
        }
      }
    }
  }
}