public function YoastSeoFieldManager::setFieldsConfiguration in Real-time SEO for Drupal 8
Set fields configuration from a form.
Explores the field present in the form and build a setting array that will be used by yoast_seo javascript.
Parameters
array $form_after_build: Node form after build.
Return value
mixed Transformed form.
File
- src/
YoastSeoFieldManager.php, line 216
Class
- YoastSeoFieldManager
- Class YoastSeoFieldManager.
Namespace
Drupal\yoast_seoCode
public function setFieldsConfiguration($form_after_build) {
$yoast_settings = $form_after_build['#yoast_settings'];
$body_field = isset($yoast_settings['body']) ? $yoast_settings['body'] : '';
$body_element = FALSE;
if ($body_field && isset($form_after_build[$body_field]['widget'][0])) {
$body_element = $form_after_build[$body_field]['widget'][0];
}
else {
return $form_after_build;
}
// Provide preview for [node:summary] even if there is no summary.
$summary_path = isset($body_element[0]['summary']) ? 'summary' : 'value';
$this->fieldsConfiguration['paths']['summary'] = $body_field . '.widget.0.' . $summary_path;
// Update field configurations.
$this->fieldsConfiguration['paths'][$body_field] = $body_field . '.widget.0.value';
$this->fieldsConfiguration['fields'][] = $body_field;
$this->fieldsConfiguration['tokens']['[node:' . $body_field . ']'] = $body_field;
$this->fieldsConfiguration['tokens']['[current-page:' . $body_field . ']'] = $body_field;
$body_format = isset($body_element['#format']) ? $body_element['#format'] : '';
// Fields requested.
// Attach settings in drupalSettings for each required field.
foreach ($this->fieldsConfiguration['fields'] as $field_name) {
$field_id = $this
->formGet($form_after_build, $this->fieldsConfiguration['paths'][$field_name] . '.#id');
if ($field_name == $body_field) {
$form_after_build['#attached']['drupalSettings']['yoast_seo']['fields']['body'] = $field_id;
}
else {
$form_after_build['#attached']['drupalSettings']['yoast_seo']['fields'][$field_name] = $field_id;
}
}
// Attach settings for the tokens.
foreach ($this->fieldsConfiguration['tokens'] as $token => $field_name) {
$form_after_build['#attached']['drupalSettings']['yoast_seo']['tokens'][$token] = $field_name;
}
// Other tokens commonly used.
$form_after_build['#attached']['drupalSettings']['yoast_seo']['tokens']['[site:name]'] = \Drupal::config('system.site')
->get('name');
$form_after_build['#attached']['drupalSettings']['yoast_seo']['tokens']['[site:slogan]'] = \Drupal::config('system.site')
->get('slogan');
$is_default_meta_title = !empty($form_after_build['field_meta_tags']['widget'][0]['basic']['title']['#default_value']) ? TRUE : FALSE;
$is_default_keyword = !empty($form_after_build['field_yoast_seo']['widget'][0]['yoast_seo']['focus_keyword']['#default_value']) ? TRUE : FALSE;
$is_default_meta_description = !empty($form_after_build['field_meta_tags']['widget'][0]['basic']['description']['#default_value']) ? TRUE : FALSE;
$body_exists = !empty($body_element['#default_value']) ? TRUE : FALSE;
// The path default value.
// @todo Should be completed once pathauto has been released for Drupal 8.
$path = '';
if (!empty($form_after_build['path']['widget'][0]['source']['#value'])) {
$path = $form_after_build['path']['widget'][0]['source']['#value'];
}
$form_after_build['#attached']['drupalSettings']['yoast_seo']['default_text'] = [
'meta_title' => $is_default_meta_title ? $form_after_build['field_meta_tags']['widget'][0]['basic']['title']['#default_value'] : '',
'keyword' => $is_default_keyword ? $form_after_build['field_yoast_seo']['widget'][0]['yoast_seo']['focus_keyword']['#default_value'] : '',
'meta_description' => $is_default_meta_description ? $form_after_build['field_meta_tags']['widget'][0]['basic']['description']['#default_value'] : '',
$body_field => $body_exists ? $body_element['#default_value'] : '',
'path' => $path,
];
// FIELDS
// Add Metatag fields.
$form_after_build['#attached']['drupalSettings']['yoast_seo']['fields']['meta_title'] = $form_after_build['field_meta_tags']['widget'][0]['basic']['title']['#id'];
$form_after_build['#attached']['drupalSettings']['yoast_seo']['fields']['meta_description'] = $form_after_build['field_meta_tags']['widget'][0]['basic']['description']['#id'];
// Placeholders.
$form_after_build['#attached']['drupalSettings']['yoast_seo']['placeholder_text'] = [
'snippetTitle' => t('Please click here to alter your page meta title'),
'snippetMeta' => t('Please click here and alter your page meta description.'),
'snippetCite' => t('/example-post'),
];
$form_after_build['#attached']['drupalSettings']['yoast_seo']['seo_title_overwritten'] = $is_default_meta_title;
$form_after_build['#attached']['drupalSettings']['yoast_seo']['text_format'] = $body_format;
// Form config.
$form_after_build['#attached']['drupalSettings']['yoast_seo']['form_id'] = $form_after_build['#id'];
return $form_after_build;
}