You are here

function cpn_form_alter in Code per Node 6

Same name and namespace in other branches
  1. 7 cpn.module \cpn_form_alter()

Implemenation of hook_form_alter().

File

./cpn.module, line 36
Primary hook implementations.

Code

function cpn_form_alter(&$form, $form_state, $form_id) {

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

    // CSS.
    if (variable_get('cpn_css_' . $form['#node']->type, FALSE) and user_access('edit css per node')) {
      $form['cpn']['css'] = array(
        '#type' => 'textarea',
        '#title' => t('CSS'),
        '#default_value' => $form['#node']->cpn['css'],
        '#description' => t('Custom CSS rules for this node. Do not include @style tags.', array(
          '@style' => '<style>',
        )),
      );
      $title[] = 'CSS';
    }

    // JS.
    if (variable_get('cpn_js_' . $form['#node']->type, FALSE) and user_access('edit javascript per node')) {
      $form['cpn']['js'] = array(
        '#type' => 'textarea',
        '#title' => t('JavaScript'),
        '#default_value' => $form['#node']->cpn['js'],
        '#description' => t('Custom JavaScript for this node. Do not include @script tags.', array(
          '@script' => '<script>',
        )),
      );
      $title[] = 'JavaScript';
    }

    // Fieldset.
    if (isset($form['cpn'])) {
      $form['cpn']['#type'] = 'fieldset';
      $form['cpn']['#title'] = t(join(' & ', $title));
      $form['cpn']['#collapsible'] = TRUE;
      $form['cpn']['#tree'] = TRUE;
      $form['cpn']['#collapsed'] = !drupal_strlen(trim($form['#node']->cpn['css'] . $form['#node']->cpn['js']));
      $form['#after_build'][] = 'cpn_after_build';
    }
  }

  // Add content type settings.
  if ($form_id == 'node_type_form' and isset($form['#node_type'])) {
    $form['cpn'] = array(
      '#type' => 'fieldset',
      '#title' => t('Code per Node settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['cpn']['cpn_css'] = array(
      '#type' => 'textarea',
      '#title' => t('CSS'),
      '#default_value' => variable_get('cpn_css_' . $form['#node_type']->type, ''),
      '#description' => t('Custom CSS rules for this content type. Do not include @style tags.', array(
        '@style' => '<style>',
      )),
    );
    $form['cpn']['cpn_js'] = array(
      '#type' => 'textarea',
      '#title' => t('JavaScript'),
      '#default_value' => variable_get('cpn_js_' . $form['#node_type']->type, ''),
      '#description' => t('Custom JavaScript for this content type. Do not include @script tags.', array(
        '@script' => '<script>',
      )),
    );
    $form['cpn']['cpn_css_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable custom CSS per node.'),
      '#return_value' => 1,
      '#default_value' => variable_get('cpn_css_enabled_' . $form['#node_type']->type, FALSE),
      '#description' => t('Users with the <em>edit node css</em> permission will be able to edit custom CSS rules per node.'),
    );
    $form['cpn']['cpn_js_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable custom JavaScript per node.'),
      '#return_value' => 1,
      '#default_value' => variable_get('cpn_js_enabled_' . $form['#node_type']->type, FALSE),
      '#description' => t('Users with the <em>edit node javascript</em> permission will be able to edit custom JavaScript per node.'),
    );
    $form['#validate'][] = 'cpn_node_type_validate';
    $form['#submit'][] = 'cpn_node_type_submit';
    $form['#after_build'][] = 'cpn_after_build';
  }

  // Block form (editing any block, or creating a Block module block).
  if ($form_id == 'block_admin_configure' or $form_id == 'block_add_block_form' and $form['module']['#value'] == 'block') {
    $title = array();

    // Load block CSS & JS.
    $cpn = array(
      'css' => '',
      'js' => '',
    );
    if (!empty($form['delta']['#value'])) {
      $cpn = db_fetch_array(db_query("SELECT css, js FROM {blocks} WHERE module = '%s' AND delta = '%s'", $form['module']['#value'], $form['delta']['#value']));
    }

    // CSS.
    if (user_access('edit css per block')) {
      $form['cpn']['css'] = array(
        '#type' => 'textarea',
        '#title' => t('CSS'),
        '#default_value' => $cpn['css'],
        '#description' => t('Custom CSS rules for this block. Do not include @style tags.', array(
          '@style' => '<style>',
        )),
      );
      $title[] = 'CSS';
    }

    // JS.
    if (user_access('edit javascript per block')) {
      $form['cpn']['js'] = array(
        '#type' => 'textarea',
        '#title' => t('JavaScript'),
        '#default_value' => $cpn['js'],
        '#description' => t('Custom JavaScript for this block. Do not include @script tags.', array(
          '@script' => '<script>',
        )),
      );
      $title[] = 'JavaScript';
    }

    // Fieldset.
    if (isset($form['cpn'])) {
      $form['cpn']['#type'] = 'fieldset';
      $form['cpn']['#title'] = t(join(' & ', $title));
      $form['cpn']['#collapsible'] = TRUE;
      $form['cpn']['#tree'] = TRUE;
      $form['cpn']['#collapsed'] = FALSE;
      $form['submit']['#weight'] = 5;
      $form['#after_build'][] = 'cpn_after_build';
      $form['#validate'][] = 'cpn_block_validate';
      $form['#submit'][] = 'cpn_block_submit';
    }
  }
}