You are here

function delta_manage_override_form in Delta 7

Same name and namespace in other branches
  1. 6 delta.module \delta_manage_override_form()
1 string reference to 'delta_manage_override_form'
delta_manage_override_page in ./delta.module
Page callback for admin/build/delta/add-override

File

./delta.module, line 523
The Delta Theme API is an advanced manipulation of the Theme Settings API to allow for customization/configuration of theme settings based on node types, context, or groups of paths.

Code

function delta_manage_override_form(&$form_state, $oid = FALSE) {

  //krumo($form_state);
  if ($oid && !$form_state['delta']) {
    $stored_data = delta_get_stored_override($oid);

    //krumo($stored_data);
  }
  $form = array();

  // Set the cache to true to create a $form_state cache on submit
  $form['#cache'] = TRUE;
  if ($oid) {
    $form['#action'] = base_path() . '/admin/build/delta/override/manage/' . $oid . '/edit';
  }
  else {
    $form['#action'] = base_path() . '/admin/build/delta/override/add';
  }
  if (isset($form_state['delta'])) {
    $delta = $form_state['delta'] + (array) $delta;
  }

  // $filter_themes tells us to only pull the themes selected in the Delta API config page, not all active themes.
  $filter_themes = TRUE;
  $theme_select = array(
    '0' => '<' . t('none') . '>',
  ) + delta_get_themes_form_array($filter_themes);

  // gather all available theme templates for the select menu
  $theme_templates = delta_get_theme_settings_templates();
  if ($delta['delta_override']['theme']) {
    $type = $delta['delta_override']['theme'];
  }
  elseif ($stored_data['delta_override']['theme']) {
    $type = $stored_data['delta_override']['theme'];
  }
  else {
    $type = 'none';
  }
  $form['delta_override'] = array(
    '#type' => 'fieldset',
    '#title' => t('Delta Override General Info'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );

  /**
   * Since there are issues with AHAH and required fields, the
   * following two fields will be marked as NOT required, but
   * manually checked during validation.
   */

  // mark this for an update rather than insert sql
  $form['delta_override']['did'] = array(
    '#type' => 'hidden',
    '#default_value' => $stored_data['delta_override']['did'],
  );
  $form['delta_override']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Override Title'),
    '#required' => FALSE,
    '#default_value' => $stored_data['delta_override']['name'],
    '#description' => t('Use a descriptive title to name your theme settings override.'),
  );
  $form['delta_override']['system_name'] = array(
    '#type' => 'textfield',
    '#title' => t('System Name'),
    '#required' => FALSE,
    '#default_value' => $stored_data['delta_override']['system_name'],
    '#description' => t('This is the unique name of the override. It must contain only alphanumeric characters and underscores'),
  );
  $form['delta_override']['theme'] = array(
    '#title' => t('Select a Theme to Override'),
    '#type' => 'select',
    '#default_value' => $stored_data['delta_override']['theme'],
    '#options' => $theme_select,
    '#ahah' => array(
      'event' => 'change',
      'path' => 'delta/theme-select-theme-settings-overrides/',
      'wrapper' => 'theme-settings-template-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#default_value' => $stored_data['delta_override']['theme'],
  );
  $form['theme_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Select Theme'),
    '#access' => FALSE,
    '#submit' => array(
      'delta_add_override_select_theme_submit',
    ),
  );
  $theme = $delta['delta_override']['theme'] ? $delta['delta_override']['theme'] : $stored_data['delta_override']['theme'];
  switch ($type) {
    case 'none':
      $msg_1 = t('You must first select a theme to apply this override to, then you will be able to select or create a theme settings template.');
      $msg_2 = t(' ');
      break;
    default:
      $msg_1 = t(' ');
      $msg_2 = t('If you do not want to use a theme settings template already in the system, you may also ') . l(t('add new template'), 'admin/build/delta/add-template/' . $theme, array(
        'attributes' => array(
          'class' => 'popups-form',
          'id' => $delta['did'],
        ),
      ));
      break;
  }

  // create an empty form element to contain the second dropdown
  $form['delta_override']['tst'] = array(
    '#tree' => TRUE,
    '#prefix' => '<div class="description" id="theme-settings-template-wrapper">',
    '#value' => $msg_1,
    '#suffix' => '</div>',
  );
  $tid = $stored_data['delta_override']['tid'];
  $form['delta_override']['tst']['theme_settings_template'] = array(
    '#title' => t('Select Theme Settings Template'),
    '#description' => $msg_2,
    '#type' => 'select',
    '#access' => $type != 'none' || $tid ? TRUE : FALSE,
    '#options' => $type == 'none' ? array(
      '0' => '<' . t('none') . '>',
    ) : $theme_templates[$type],
    '#default_value' => $tid ? $tid : 0,
  );
  $form['delta_override']['weight'] = array(
    '#type' => 'weight',
    '#description' => t('The weight field will prioritize this override, and its importance in ordering overrides for processing. The lower the weight, the earlier in the process it occurs.'),
    '#default_value' => $stored_data['delta_override']['weight'] ? $stored_data['delta_override']['weight'] : 0,
    '#delta' => 100,
  );
  $form['delta_conditions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Delta Override Conditional Criteria'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $stored_nodes = $stored_data['delta_conditions']['nodes'] ? $stored_data['delta_conditions']['nodes'] : array();
  $form['delta_conditions']['nodes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node Types'),
    '#options' => node_get_types('names'),
    '#default_value' => $stored_nodes,
    '#description' => t('You may select as many node types as you like here. Any node types selected will have this override applied to them unless a higher priority override applies to the same node type.'),
  );

  /**
   * If we have the context module installed, we will enable a list to select those
   * contexts that the override should apply to. </rockstar-stuff>
   */
  if (module_exists('context')) {
    $contexts = context_enabled_contexts();
    $form_contexts = array();
    if (count($contexts > 0)) {
      foreach ($contexts as $key => $context) {
        $namespace = $context->namespace;
        $attribute = $context->attribute;
        $value = $context->value;
        $form_contexts[$context->cid] = $namespace . ' &gt; ' . $attribute . ' &gt; ' . $value;
      }
    }
    $stored_contexts = $stored_data['delta_conditions']['contexts'] ? $stored_data['delta_conditions']['contexts'] : array();
    $form['delta_conditions']['contexts'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Contexts'),
      '#options' => $form_contexts,
      '#default_value' => $stored_contexts,
      '#description' => t('You may select as many contexts as you like here. Any node types selected will have this override applied to them unless a higher priority override applies to the same context.'),
    );
  }
  $form['delta_conditions']['paths'] = array(
    '#type' => 'textarea',
    '#title' => t('Paths'),
    '#default_value' => $stored_data['delta_conditions']['paths'],
    '#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-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );

  // Since a #select element can't have it's own #submit handler, we have to create an extra
  // submit button to which we attach the correct callback function that's going to be called on a change
  // We'll hide this with css! This is taken care of in ahah_example_init().
  // submit button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Override'),
    '#validate' => array(
      'delta_add_override_validate',
    ),
    '#submit' => array(
      'delta_add_override_submit',
    ),
  );
  return $form;
}