You are here

function page_title_form_alter in Page Title 6.2

Same name and namespace in other branches
  1. 8.2 page_title.module \page_title_form_alter()
  2. 5.2 page_title.module \page_title_form_alter()
  3. 5 page_title.module \page_title_form_alter()
  4. 6 page_title.module \page_title_form_alter()
  5. 7.2 page_title.module \page_title_form_alter()
  6. 7 page_title.module \page_title_form_alter()

Implementation of hook_form_alter().

File

./page_title.module, line 193
Enhanced control over the page title (in the head tag).

Code

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

  // If we dont have permission to set the title then we need to abort this alter now!
  if (!user_access('set page title')) {
    return;
  }

  // Check we're editing a node and also check that the node type's 'show field' is enabled
  if ($form['#id'] == 'node-form') {
    $key = 'page_title_type_' . $form['type']['#value'] . '_showfield';
    if (variable_get($key, 0)) {
      $page_title = isset($form['#node']->page_title) ? $form['#node']->page_title : NULL;

      // If we have vertical tabs installed, we need to render the form element slightly differently
      $show_vertical_tabs = FALSE;
      if (module_exists('vertical_tabs')) {
        if (arg(0) == 'admin') {

          // If its an admin page, we must render it as a fieldset - the vertical tabs allows per-fieldset disabling. We have to render a fieldset to allow the option to disable it.
          $show_vertical_tabs = TRUE;
        }
        else {

          // This isn't an admin page, we should decide whether to render the page title fieldset depending on if the setting is set.
          $show_vertical_tabs = ($vt_conf = vertical_tabs_get_config($form_id)) && $vt_conf['page_title'] !== 0;
        }
      }

      // If we have decided to show vertical tabs, render the page_title element into a fieldset, otherwise just a textfield with a weight putting it at the top.
      if ($show_vertical_tabs) {
        $form['page_title'] = array(
          '#type' => 'fieldset',
          '#title' => t('Page Title settings'),
          '#collapsible' => TRUE,
          '#collapsed' => empty($page_title),
          '#group' => 'additional_settings',
          '#weight' => 35,
          '#attached' => array(
            'js' => array(
              'vertical-tabs' => drupal_get_path('module', 'page_title') . '/page_title.js',
            ),
          ),
        );
        $form['page_title']['page_title'] = array(
          '#type' => 'textfield',
          '#title' => t('Page title'),
          '#description' => t('Provide a description of this node to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'),
          '#default_value' => $page_title,
          '#size' => 60,
          '#maxlength' => 255,
        );
      }
      else {
        $form['page_title'] = array(
          '#type' => 'textfield',
          '#title' => t('Page title'),
          '#description' => t('Provide a description of this node to appear in the <title> tag which search engines can use in search result listings (optional). It is generally accepted this field should be less than 70 characters.'),
          '#default_value' => $page_title,
          '#size' => 60,
          '#maxlength' => 255,
          '#weight' => -4,
        );
      }
      drupal_add_js(drupal_get_path('module', 'page_title') . '/page_title.js', 'module', 'header', FALSE, TRUE, FALSE);
    }
  }
}