You are here

function yoast_seo_configuration_form_after_build in Real-time SEO for Drupal 7

In this afterbuild function we add the configuration to the JavaScript.

At this point we have access to the HTML id's of form elements we need for the JavaScript to function.

1 string reference to 'yoast_seo_configuration_form_after_build'
yoast_seo_configuration_form in ./yoast_seo.module
Build a FAPI array for editing meta tags.

File

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

Code

function yoast_seo_configuration_form_after_build($form, &$form_state) {

  // Work out the language code to use, default to NONE. This is used on the
  // yoast_seo settings per language and for metatags..
  if (!empty($form['#entity_type'])) {
    if (!empty($form['#entity'])) {
      $langcode = entity_language($form['#entity_type'], (object) $form['#entity']);
    }
  }
  if (empty($langcode)) {
    $langcode = LANGUAGE_NONE;
  }

  // Lets put the container in the bottom of page, above additional_settings
  // tabs.
  $form['yoast_seo']['#weight'] = $form['additional_settings']['#weight'] - 2;

  // Generate unique HTML IDs.
  $wrapper_target_id = drupal_html_id('yoast-wrapper');
  $output_target_id = drupal_html_id('yoast-output');
  $overallscore_target_id = drupal_html_id('yoast-overallscore');
  $snippet_target_id = drupal_html_id('yoast-snippet');
  $score = !empty($form['yoast_seo'][$langcode]['seo_status']['#value']) ? yoast_seo_score_rating($form['yoast_seo'][$langcode]['seo_status']['#value']) : yoast_seo_score_rating(0);

  // Add the Yoast SEO title before the wrapper.
  $form['yoast_seo'][$langcode]['focus_keyword']['#prefix'] = '<h3 class="wrapper-title">' . t('Real-time SEO for Drupal') . '</h3>';

  // Output the overall score next to the focus keyword.
  $form['yoast_seo'][$langcode]['focus_keyword']['#suffix'] = '<div id="yoast-overallscore" class="overallScore ' . $score . '">
  <div class="score_circle"></div>
  <span class="score_title">' . t('SEO') . ': <strong>' . $score . '</strong></span>
  </div>';

  // Output the markup for the snippet and overall score above the body field.
  $form['yoast_seo'][$langcode]['snippet_analysis'] = array(
    '#weight' => $form['yoast_seo']['#weight'] - 1,
    '#markup' => '<div id="' . $wrapper_target_id . '"><div id="wpseo_meta"></div><label>' . t('Snippet editor') . '</label><div id="' . $snippet_target_id . '"></div><label>' . t('Content analysis') . '</label><div id="' . $output_target_id . '"></div></div>',
  );

  // Minified JS file.
  drupal_add_js(yoast_seo_library_path('js-text-analysis') . '/yoast-seo.min.js', array(
    'type' => 'external',
    'scope' => 'footer',
  ));

  // Our own JavaScript.
  drupal_add_js(drupal_get_path('module', 'yoast_seo') . '/js/yoast_seo.js');

  // Our own CSS.
  drupal_add_css(drupal_get_path('module', 'yoast_seo') . '/css/yoast_seo.css');
  global $base_root;
  $default_url = '';
  if (!empty($form['path']['alias']['#default_value'])) {
    $default_url = $form['path']['alias']['#default_value'];
  }
  elseif (!empty($form['path']['source']['#value'])) {
    $default_url = $form['path']['source']['#value'];
  }

  // Check if the SEO title field is overwritten.
  $seo_title_overwritten = FALSE;
  if (!empty($form['metatags'][$langcode]['basic']['title']['value']['#default_value'])) {
    $seo_title_overwritten = TRUE;
  }

  // Collect all body fields and field id's.
  $text_content = array();
  if (isset($form['body'])) {

    // We use the language of the body element so we know where to add the
    // correct yoast_seo settings / fields and elements.
    $body_language = $form['body']['#language'];
    $text_content += _yoast_seo_process_field($form['body'][$body_language]);
  }
  $yoast_fields = variable_get('yoast_seo_body_fields_' . $form['#bundle'], array());
  foreach ($yoast_fields as $yoast_field) {

    // We use the language of the field element.
    $field_language = $form[$yoast_field]['#language'];
    $text_content += _yoast_seo_process_field($form[$yoast_field][$field_language]);
  }

  // Create the configuration we will sent to the content analysis library.
  $configuration = array(
    'analyzer' => TRUE,
    'snippetPreview' => TRUE,
    'targetId' => $wrapper_target_id,
    'targets' => array(
      'output' => $output_target_id,
      'overall' => $overallscore_target_id,
      'snippet' => $snippet_target_id,
    ),
    'defaultText' => array(
      'url' => $default_url,
      'title' => !empty($form['metatags'][$langcode]['basic']['title']['value']['#default_value']) ? $form['metatags'][$langcode]['basic']['title']['value']['#default_value'] : '',
      'keyword' => !empty($form['yoast_seo'][$langcode]['focus_keyword']['#default_value']) ? $form['yoast_seo'][$langcode]['focus_keyword']['#default_value'] : '',
      'meta' => !empty($form['metatags'][$langcode]['basic']['description']['value']['#default_value']) ? $form['metatags'][$langcode]['basic']['description']['value']['#default_value'] : '',
      'body' => trim(implode(PHP_EOL, $text_content)),
    ),
    'fields' => array(
      'focusKeyword' => $form['yoast_seo'][$langcode]['focus_keyword']['#id'],
      'seoStatus' => $form['yoast_seo'][$langcode]['seo_status']['#attributes']['id'],
      'pageTitle' => module_exists('seo_ui') ? $form['seo_vtab']['metatags_title']['title']['value']['#id'] : $form['metatags'][$langcode]['basic']['title']['value']['#id'],
      'nodeTitle' => $form['title']['#id'],
      'description' => module_exists('seo_ui') ? $form['seo_vtab']['metatags'][$langcode]['basic']['description']['value']['#id'] : $form['metatags'][$langcode]['basic']['description']['value']['#id'],
      'bodyText' => array_keys($text_content),
      'url' => module_exists('seo_ui') ? $form['seo_vtab']['path']['alias']['#id'] : $form['path']['alias']['#id'],
    ),
    'placeholderText' => array(
      'title' => t('Please click here to alter your page meta title'),
      'description' => t('Please click here and alter your page meta description.'),
      'url' => t('example-post'),
    ),
    'SEOTitleOverwritten' => $seo_title_overwritten,
    'textFormat' => !empty($form['body'][$langcode][0]['format']['#id']) ? $form['body'][$langcode][0]['format']['#id'] : '',
    'baseRoot' => $base_root,
  );

  // Allow other modules to alter the configuration we sent to the content
  // analysis library by implementing hook_yoast_seo_configuration_alter().
  drupal_alter('yoast_seo_configuration', $configuration);

  // Add the configuration to the front-end for the content analysis library.
  drupal_add_js(array(
    'yoast_seo' => $configuration,
  ), 'setting');
  return $form;
}