View source
<?php
namespace Drupal\yoast_seo;
use Drupal\Component\Utility\NestedArray;
class YoastSeoFieldManager {
public $fieldsConfiguration = [
'paths' => [
'title' => 'title.widget.0.value',
'focus_keyword' => 'field_yoast_seo.widget.0.yoast_seo.focus_keyword',
'seo_status' => 'field_yoast_seo.widget.0.yoast_seo.status',
'path' => 'path.widget.0.alias',
],
'fields' => [
'title',
'summary',
'focus_keyword',
'seo_status',
'path',
],
'tokens' => [
'[current-page:title]' => 'title',
'[node:title]' => 'title',
'[current-page:summary]' => 'summary',
'[node:summary]' => 'summary',
],
];
protected $logger;
protected $entity_manager;
public function __construct() {
$this->entity_manager = \Drupal::service('entity_type.manager');
}
private function formSet(&$form, $key, $value) {
return NestedArray::setValue($form, explode('.', $key), $value);
}
private function formGet($form, $key) {
return NestedArray::getValue($form, explode('.', $key));
}
public function attachField($entity_type, $bundle, $field) {
$field_storage_config = $this->entity_manager
->getStorage('field_storage_config')
->load($entity_type . '.' . $field['field_name']);
if (is_null($field_storage_config)) {
$this->entity_manager
->getStorage('field_storage_config')
->create([
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'type' => $field['storage_type'],
'translatable' => $field['translatable'],
])
->save();
}
$fields_config = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle);
if (!isset($fields_config[$field['field_name']])) {
$field_values = [
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $field['field_label'],
'translatable' => $field['translatable'],
];
$this->entity_manager
->getStorage('field_config')
->create($field_values)
->save();
$this->entity_manager
->getStorage('entity_form_display')
->load($entity_type . '.' . $bundle . '.default')
->setComponent($field['field_name'], [])
->save();
$this->entity_manager
->getStorage('entity_view_display')
->load($entity_type . '.' . $bundle . '.default')
->setComponent($field['field_name'], [])
->save();
}
}
public function detachField($entity_type, $bundle, $field_name) {
$fields_config = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle);
if (isset($fields_config[$field_name])) {
$fields_config[$field_name]
->delete();
}
}
public function isAttached($entity_type, $bundle, $field_name) {
$fields_config = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle);
return isset($fields_config[$field_name]);
}
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;
}
$summary_path = isset($body_element[0]['summary']) ? 'summary' : 'value';
$this->fieldsConfiguration['paths']['summary'] = $body_field . '.widget.0.' . $summary_path;
$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'] : '';
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;
}
}
foreach ($this->fieldsConfiguration['tokens'] as $token => $field_name) {
$form_after_build['#attached']['drupalSettings']['yoast_seo']['tokens'][$token] = $field_name;
}
$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;
$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,
];
$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'];
$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_after_build['#attached']['drupalSettings']['yoast_seo']['form_id'] = $form_after_build['#id'];
return $form_after_build;
}
public function addSnippetEditorMarkup($form) {
$yoast_settings = $form['#yoast_settings'];
$body_field = isset($yoast_settings['body']) ? $yoast_settings['body'] : '';
$yoast_seo_manager = \Drupal::service('yoast_seo.manager');
$output = $yoast_seo_manager
->getSnippetEditorMarkup();
$body_weight = isset($form[$body_field]['#weight']) ? $form[$body_field]['#weight'] : 20;
$this
->formSet($form, 'field_yoast_seo.widget.0.yoast_seo.snippet_analysis', [
'#weight' => $body_weight + 1,
'#markup' => $output,
'#parents' => [],
]);
return $form;
}
public function addOverallScoreMarkup($form, &$form_state) {
$yoast_seo_manager = \Drupal::service('yoast_seo.manager');
$field_value = $form_state
->getFormObject()
->getEntity()
->get('field_yoast_seo')
->getValue();
$score = isset($field_value[0]['status']) ? $field_value[0]['status'] : 0;
$output = $yoast_seo_manager
->getOverallScoreMarkup($score);
$this
->formSet($form, 'field_yoast_seo.widget.0.yoast_seo.focus_keyword.#suffix', $output);
return $form;
}
}