You are here

function yoast_seo_configuration_form in Real-time SEO for Drupal 7

Build a FAPI array for editing meta tags.

Parameters

array $form: The current FAPI array.

string $instance: The configuration instance key to use, e.g. "node:article".

array $options: (optional) An array of options including the following keys and values:

1 call to yoast_seo_configuration_form()
yoast_seo_form_alter in ./yoast_seo.module
Implements hook_form_alter().

File

./yoast_seo.module, line 62
Primary hook implementations for Yoast SEO for Drupal module.

Code

function yoast_seo_configuration_form(array &$form, $instance, array $options = array()) {

  // Making sure we have the necessary form elements and we have access to the
  // form elements. Otherwise we're executing code we'll never use.
  if (!empty($form['path']['#access']) && !empty($form['metatags']['#access']) && (user_access('use yoast seo') || user_access('administer yoast seo'))) {

    // Work out the language code to use, default to NONE.
    if (!empty($form['#entity_type'])) {
      if (!empty($form['#entity'])) {
        $langcode = entity_language($form['#entity_type'], (object) $form['#entity']);
      }
      if (empty($langcode)) {
        $langcode = LANGUAGE_NONE;
      }
    }

    // Merge in the default options.
    $options += array(
      'instance' => $instance,
    );
    $form['yoast_seo'] = array(
      '#type' => 'container',
      '#title' => t('Real-time SEO'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#multilingual' => TRUE,
      '#tree' => TRUE,
      '#weight' => 35,
      '#language' => $langcode,
      '#attributes' => array(
        'class' => array(
          'yoast-seo-form',
        ),
      ),
    );
    $form['yoast_seo'][$langcode] = array(
      '#type' => 'container',
      '#multilingual' => TRUE,
      '#tree' => TRUE,
    );

    // Put Yoast in a vertical tab if setting is set.
    $yoast_seo_vertical_tab = variable_get('yoast_seo_vertical_tab');
    if (!empty($yoast_seo_vertical_tab)) {
      $form['yoast_seo']['#type'] = 'fieldset';
    }

    // Only support vertical tabs if there is a vertical tab element.
    foreach (element_children($form) as $key) {
      if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
        $form['yoast_seo']['#group'] = $key;
        break;
      }
    }
    $metatag_config = metatag_config_load_with_defaults($options['instance']);
    $new_entity = empty($form['#entity']->nid) ? TRUE : FALSE;

    // Add the focus keyword field.
    $form['yoast_seo'][$langcode]['focus_keyword'] = array(
      '#type' => 'textfield',
      '#title' => t('Focus keyword'),
      '#description' => t('Pick the main keyword or keyphrase that this post/page is about.'),
      '#default_value' => !empty($form['#entity']->yoast_seo[$langcode]['focus_keyword']) ? $form['#entity']->yoast_seo[$langcode]['focus_keyword'] : '',
      '#id' => drupal_html_id('focus-keyword'),
    );

    // Add the SEO status field.
    $form['yoast_seo'][$langcode]['seo_status'] = array(
      '#type' => 'hidden',
      '#default_value' => !empty($form['#entity']->yoast_seo[$langcode]['seo_status']) ? $form['#entity']->yoast_seo[$langcode]['seo_status'] : 0,
      '#attributes' => array(
        'id' => drupal_html_id('seo-status'),
      ),
    );

    // Set placeholder tekst if there is a default in metatags and it is a new
    // entity. This means we have some default configuration for the title.
    if (!empty($form['metatags'][$langcode]['basic']['title'])) {

      // Checking to see if we have to empty the default value or not.
      if ($form['metatags'][$langcode]['basic']['title']['value']['#default_value'] == $metatag_config['title']['value']) {
        if ($new_entity) {
          $form['metatags'][$langcode]['basic']['title']['value']['#default_value'] = $metatag_config['title']['value'];
        }
        else {
          $form['metatags'][$langcode]['basic']['title']['value']['#default_value'] = token_replace($metatag_config['title']['value'], array(
            'node' => $form['#entity'],
          ));
        }
      }
    }

    // Set placeholder tekst if there is a default in metatags and it is a new
    // entity. This means we have some default configuration for the
    // description.
    if (!empty($form['metatags'][$langcode]['basic']['description']) && isset($metatag_config['description'])) {

      // Checking to see if we have to empty the default value or not.
      if ($form['metatags'][$langcode]['basic']['description']['value']['#default_value'] == $metatag_config['description']['value']) {
        if ($new_entity) {
          $form['metatags'][$langcode]['basic']['description']['value']['#default_value'] = $metatag_config['description']['value'];
        }
        else {
          $form['metatags'][$langcode]['basic']['description']['value']['#default_value'] = strip_tags(token_replace($metatag_config['description']['value'], array(
            'node' => $form['#entity'],
          )));
        }
      }
    }

    // Add a form after build to add the JavaScript.
    $form['#after_build'][] = 'yoast_seo_configuration_form_after_build';

    // Add a submit handler to compare the submitted values against the default
    // values.
    $form += array(
      '#submit' => array(),
    );
    array_unshift($form['#submit'], 'yoast_seo_configuration_form_submit');
  }
}