You are here

function context_node_form_alter in Context Node 6

Same name and namespace in other branches
  1. 7 context_node.module \context_node_form_alter()

Implements hook_form_alter()

File

./context_node.module, line 15

Code

function context_node_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    $form['context'] = array(
      '#type' => 'fieldset',
      '#title' => t("Allowed node contexts"),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 10,
    );
    $form['context']['context_node'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Select allowed contents'),
      '#options' => _context_node_get_contexts(),
      '#description' => t('Select all contexts that will be available for this content type'),
    );
    if (variable_get('context_node_' . $form['#node_type']->type, '') != NULL) {
      $form['context']['context_node']['#default_value'] = variable_get('context_node_' . $form['#node_type']->type, '');
    }
    $default = variable_get('context_node_default_' . $form['#node_type']->type, FALSE);
    $form['context']['context_node_default'] = array(
      '#type' => 'radios',
      '#title' => t('Select the default context'),
      '#default_value' => isset($default) ? $default : "none",
      '#options' => _context_node_get_default_contexts(),
      '#description' => t('Select the default context. If you select "Disabled" this functionality will be disabled for this content type. If you select "Default" the functionality will be enabled but no context will be enabled by default'),
    );
  }

  // Node Form
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
    $node = $form['#node'];

    // Check if this content type is enabled to use 'context node'
    $option = variable_get("context_node_default_" . $node->type, '');
    if ($option == "none") {
      return;
    }
    if (!empty($option)) {
      $form['context_node'] = array(
        '#type' => 'fieldset',
        '#title' => t('Context'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#access' => user_access('set context on nodes'),
        '#weight' => 130,
      );
      $options = _context_node_get_contexts_node_type($node->type);
      $default = variable_get("context_node_default_" . $node->type, '');
      $form['context_node']['context'] = array(
        '#type' => 'radios',
        '#title' => t('Context'),
        '#description' => t('Select a context from the list to change the layout and configuration of this !type', array(
          '!type' => $node->type,
        )),
        '#default_value' => isset($node->context) ? $node->context : $default,
        '#options' => $options,
        '#access' => user_access('set context on nodes'),
        '#submit' => array(
          'context_node_form_submit',
        ),
      );
    }
  }
}