You are here

function submenutree_form_alter in Submenu Tree 7.2

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

Implements hook_form_alter().

File

./submenutree.module, line 89
Primarily Drupal hooks and processing the Submenu Tree display.

Code

function submenutree_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form' || !empty($form['#node_edit_form']) && variable_get('submenutree_node_type_' . $form['type']['#value'], 0)) {
    $form['submenutree'] = array(
      '#type' => 'fieldset',
      '#group' => 'additional_settings',
      '#title' => t('Sub and Sibling content'),
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'submenutree') . '/submenutree.js',
        ),
      ),
      '#attributes' => array(
        'class' => array(
          'node-form-submenutree-settings',
        ),
      ),
    );
    $form['submenutree']['submenutree_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display sub content'),
    );
    $form['submenutree']['submenutree_container'] = array(
      '#type' => 'container',
    );
    $form['submenutree']['submenutree_container']['submenutree_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#description' => t('The title of the sub content and sub content block.'),
    );
    $form['submenutree']['submenutree_container']['submenutree_display'] = array(
      '#type' => 'select',
      '#title' => t('Display type'),
      '#options' => array(
        'content' => array(
          SUBMENUTREE_DISPLAY_MENU => t('Menu'),
          SUBMENUTREE_DISPLAY_TITLES => t('Titles only'),
          SUBMENUTREE_DISPLAY_VIEW_MODE => t('Select view mode'),
        ),
        'block' => array(
          SUBMENUTREE_DISPLAY_BLOCK_MENU => t('Menu'),
          SUBMENUTREE_DISPLAY_BLOCK_TITLES => t('Titles only'),
          SUBMENUTREE_DISPLAY_BLOCK_VIEW_MODE => t('Select view mode'),
        ),
      ),
      '#description' => t('Select where and how the sub content should be displayed.'),
    );
    $entity_info = entity_get_info('node');
    foreach ($entity_info['view modes'] as $view_mode => $view_mode_info) {
      $view_modes[$view_mode] = $view_mode_info['label'];
    }
    $form['submenutree']['submenutree_container']['submenutree_view_mode'] = array(
      '#type' => 'select',
      '#title' => t('View mode'),
      '#description' => t('Select a the view mode to render the menu items in.'),
      '#options' => $view_modes,
      '#states' => array(
        'visible' => array(
          ':input[name="submenutree_display"]' => array(
            'value' => SUBMENUTREE_DISPLAY_VIEW_MODE,
          ),
        ),
      ),
    );
    $form['submenutree']['submenutree_container']['submenutree_links'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show links'),
      '#states' => array(
        'visible' => array(
          ':input[name="submenutree_display"]' => array(
            'value' => SUBMENUTREE_DISPLAY_VIEW_MODE,
          ),
        ),
      ),
    );
    $form['submenutree']['siblingmenutree_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display sibling content'),
      '#default_value' => !empty($node->siblingmenutree_enable),
    );
    $form['submenutree']['siblingmenutree_container'] = array(
      '#type' => 'container',
    );
    $form['submenutree']['siblingmenutree_container']['siblingmenutree_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#description' => t('The title of the sibling content and sibling content block.'),
    );
    $form['submenutree']['siblingmenutree_container']['siblingmenutree_display'] = array(
      '#type' => 'select',
      '#title' => t('Display type'),
      '#options' => array(
        'content' => array(
          SUBMENUTREE_DISPLAY_MENU => t('Menu'),
          SUBMENUTREE_DISPLAY_TITLES => t('Titles only'),
          SUBMENUTREE_DISPLAY_VIEW_MODE => t('Select view mode'),
        ),
        'block' => array(
          SUBMENUTREE_DISPLAY_BLOCK_MENU => t('Menu'),
          SUBMENUTREE_DISPLAY_BLOCK_TITLES => t('Titles only'),
          SUBMENUTREE_DISPLAY_BLOCK_VIEW_MODE => t('Select view mode'),
        ),
      ),
      '#description' => t('Select where and how the sibling content should be displayed.'),
    );
    $form['submenutree']['siblingmenutree_container']['siblingmenutree_view_mode'] = array(
      '#type' => 'select',
      '#title' => t('View mode'),
      '#description' => t('Select a the view mode to render the menu items in.'),
      '#options' => $view_modes,
      '#states' => array(
        'visible' => array(
          ':input[name="siblingmenutree_display"]' => array(
            'value' => SUBMENUTREE_DISPLAY_VIEW_MODE,
          ),
        ),
      ),
    );
    $form['submenutree']['siblingmenutree_container']['siblingmenutree_links'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show links'),
      '#states' => array(
        'visible' => array(
          ':input[name="siblingmenutree_display"]' => array(
            'value' => SUBMENUTREE_DISPLAY_VIEW_MODE,
          ),
        ),
      ),
    );

    // Default values and access restrictions when editing content types
    if ($form_id == 'node_type_form') {

      // Restrict access to Submenu Tree configuration settings
      $form['submenutree']['#access'] = user_access('administer submenu tree settings');

      // Add a set of radio buttons to indicate which content types should have submenu tree options added
      $form['submenutree']['submenutree_node_type'] = array(
        '#type' => 'radios',
        '#title' => t('Submenu Tree'),
        '#default_value' => variable_get('submenutree_node_type_' . $form['#node_type']->type, 0),
        '#options' => array(
          1 => t('Enabled'),
          0 => t('Disabled'),
        ),
        '#description' => t('Enable or disable Submenu Tree for this node type.'),
        '#weight' => -50,
      );

      // Set content type specific sub content settings
      $form['submenutree']['submenutree_enable']['#default_value'] = variable_get('submenutree_enable_' . $form['#node_type']->type, 0);
      $form['submenutree']['submenutree_enable']['#states'] = array(
        'visible' => array(
          ':input[name="submenutree_node_type"]' => array(
            'value' => 1,
          ),
        ),
      );
      $form['submenutree']['submenutree_container']['#states'] = array(
        'visible' => array(
          ':input[name="submenutree_node_type"]' => array(
            'value' => 1,
          ),
        ),
      );
      $form['submenutree']['submenutree_container']['submenutree_title']['#default_value'] = variable_get('submenutree_title_' . $form['#node_type']->type, '');
      $form['submenutree']['submenutree_container']['submenutree_display']['#default_value'] = variable_get('submenutree_display_' . $form['#node_type']->type, SUBMENUTREE_DISPLAY_MENU);
      $form['submenutree']['submenutree_container']['submenutree_view_mode']['#default_value'] = variable_get('submenutree_view_mode_' . $form['#node_type']->type, 'full');
      $form['submenutree']['submenutree_container']['submenutree_links']['#default_value'] = variable_get('submenutree_links_' . $form['#node_type']->type, FALSE);

      // Set content type specific sibling content settings
      $form['submenutree']['siblingmenutree_enable']['#default_value'] = variable_get('siblingmenutree_enable_' . $form['#node_type']->type, 0);
      $form['submenutree']['siblingmenutree_enable']['#states'] = array(
        'visible' => array(
          ':input[name="submenutree_node_type"]' => array(
            'value' => 1,
          ),
        ),
      );
      $form['submenutree']['siblingmenutree_container']['#states'] = array(
        'visible' => array(
          ':input[name="submenutree_node_type"]' => array(
            'value' => 1,
          ),
        ),
      );
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_title']['#default_value'] = variable_get('siblingmenutree_title_' . $form['#node_type']->type, '');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_display']['#default_value'] = variable_get('siblingmenutree_display_' . $form['#node_type']->type, SUBMENUTREE_DISPLAY_MENU);
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_view_mode']['#default_value'] = variable_get('siblingmenutree_view_mode_' . $form['#node_type']->type, 'full');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_links']['#default_value'] = variable_get('siblingmenutree_links_' . $form['#node_type']->type, FALSE);
    }

    // Default values and access restrictions when editing nodes
    if (!empty($form['#node_edit_form'])) {

      // Node
      $node = $form['#node'];

      // Restrict access to the node's sub/sibling content settings
      $form['submenutree']['#access'] = user_access('administer sub content') || user_access('administer sibling content');

      // Set node specific sub content settings
      $form['submenutree']['submenutree_enable']['#access'] = user_access('administer sub content');
      $form['submenutree']['submenutree_container']['submenutree_title']['#access'] = user_access('administer sub content title');
      $form['submenutree']['submenutree_container']['submenutree_display']['#access'] = user_access('administer sub content display type');
      $form['submenutree']['submenutree_container']['submenutree_view_mode']['#access'] = user_access('administer sub content view mode');
      $form['submenutree']['submenutree_container']['submenutree_links']['#access'] = user_access('administer sub content view mode');
      $form['submenutree']['submenutree_enable']['#default_value'] = isset($node->submenutree_enable) ? $node->submenutree_enable : variable_get('submenutree_enable_' . $node->type, 0);
      $form['submenutree']['submenutree_container']['#states'] = array(
        'invisible' => array(
          ':input[name="submenutree_enable"]' => array(
            'checked' => FALSE,
          ),
        ),
      );
      $form['submenutree']['submenutree_container']['submenutree_title']['#default_value'] = isset($node->submenutree_title) ? $node->submenutree_title : variable_get('submenutree_title_' . $node->type, '');
      $form['submenutree']['submenutree_container']['submenutree_display']['#default_value'] = isset($node->submenutree_display) ? $node->submenutree_display : variable_get('submenutree_display_' . $node->type, SUBMENUTREE_DISPLAY_MENU);
      $form['submenutree']['submenutree_container']['submenutree_view_mode']['#default_value'] = isset($node->submenutree_view_mode) ? $node->submenutree_view_mode : variable_get('submenutree_view_mode_' . $node->type, 'full');
      $form['submenutree']['submenutree_container']['submenutree_links']['#default_value'] = isset($node->submenutree_links) ? $node->submenutree_links : variable_get('submenutree_links_' . $node->type, FALSE);

      // Set node type specific sibling content settings
      $form['submenutree']['siblingmenutree_enable']['#access'] = user_access('administer sibling content');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_title']['#access'] = user_access('administer sibling content title');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_display']['#access'] = user_access('administer sibling content display type');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_view_mode']['#access'] = user_access('administer sibling content view mode');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_links']['#access'] = user_access('administer sibling content view mode');
      $form['submenutree']['siblingmenutree_enable']['#default_value'] = isset($node->siblingmenutree_enable) ? $node->siblingmenutree_enable : variable_get('siblingmenutree_enable_' . $node->type, 0);
      $form['submenutree']['siblingmenutree_container']['#states'] = array(
        'invisible' => array(
          ':input[name="siblingmenutree_enable"]' => array(
            'checked' => FALSE,
          ),
        ),
      );
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_title']['#default_value'] = isset($node->siblingmenutree_title) ? $node->siblingmenutree_title : variable_get('siblingmenutree_title_' . $node->type, '');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_display']['#default_value'] = isset($node->siblingmenutree_display) ? $node->siblingmenutree_display : variable_get('siblingmenutree_display_' . $node->type, SUBMENUTREE_DISPLAY_MENU);
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_view_mode']['#default_value'] = isset($node->siblingmenutree_view_mode) ? $node->siblingmenutree_view_mode : variable_get('siblingmenutree_view_mode_' . $node->type, 'full');
      $form['submenutree']['siblingmenutree_container']['siblingmenutree_links']['#default_value'] = isset($node->siblingmenutree_links) ? $node->siblingmenutree_links : variable_get('siblingmenutree_links_' . $node->type, FALSE);
    }
  }
}