You are here

function cpn_form_node_form_alter in Code per Node 7

Implements hook_form_BASE_FORM_ID_alter().

File

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

Code

function cpn_form_node_form_alter(&$form, $form_state) {
  $title = array();
  $cpn = !empty($form['#node']->cpn) ? $form['#node']->cpn : array(
    'css' => '',
    'js' => '',
    'noscript' => '',
  );

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

    // Indicate if per-content-type code will be loaded.
    $file = variable_get('cpn_path', 'public://cpn') . '/' . $form['#node']->type . '.css';
    $files_dir = variable_get('file_public_path', conf_path() . '/files');
    $file = $files_dir . '/' . file_uri_target($file);
    if (file_exists($file)) {
      $form['cpn']['css']['#description'] .= '<br />' . t('The following CSS file will also be loaded for all nodes of this content type: !file<br />', array(
        '!file' => l($file, $file),
      ));
    }
  }

  // JS.
  if (variable_get('cpn_js_enabled_' . $form['#node']->type, FALSE) && (user_access('administer code per node') || user_access('edit javascript per node'))) {
    $form['cpn']['js'] = array(
      '#type' => 'textarea',
      '#title' => t('JavaScript'),
      '#default_value' => $cpn['js'],
      '#description' => t('Custom JavaScript for this node. Do not include @script tags.', array(
        '@script' => '<script>',
      )),
    );
    $form['cpn']['noscript'] = array(
      '#type' => 'textarea',
      '#title' => t('NOSCRIPT'),
      '#default_value' => $cpn['noscript'],
      '#description' => t("Custom HTML to show if JavaScript is not enabled in the visitor's browser. Do not include @noscript tags.", array(
        '@noscript' => '<noscript>',
      )),
    );
    $title[] = 'JavaScript';

    // Indicate if per-content-type code will be loaded.
    $file = variable_get('cpn_path', 'public://cpn') . '/' . $form['#node']->type . '.js';
    $files_dir = variable_get('file_public_path', conf_path() . '/files');
    $file = $files_dir . '/' . file_uri_target($file);
    if (file_exists($file)) {
      $form['cpn']['js']['#description'] .= '<br />' . t('The following JavaScript file will also be loaded for all nodes of this content type: !file<br />', array(
        '!file' => l($file, $file),
      ));
    }
  }

  // Fieldset.
  if (isset($form['cpn'])) {
    $form['cpn']['#type'] = 'fieldset';
    $form['cpn']['#title'] = t(join(' & ', $title));
    $form['cpn']['#tree'] = TRUE;
    $form['cpn']['#group'] = 'additional_settings';
    cpn_attach_syntax_highlighting($form['cpn'], isset($form['cpn']['css']), isset($form['cpn']['js']));

    // Show the list of available tokens.
    if (module_exists('token')) {
      $form['cpn']['tokens'] = array(
        '#prefix' => '<p>' . t('Custom tokens may be added to the code to output certain information.') . '</p>',
        '#theme' => 'token_tree',
        '#token_types' => array(
          'node',
        ),
        '#weight' => 999,
        '#dialog' => TRUE,
      );
    }
  }
}