Commit 3eb14eb4 authored by Kingdutch's avatar Kingdutch

Issue #2941887 by Kingdutch: yoast_seo_metatags_alter relies on removed code

The new yoast_seo_metatags_alter searches for a yoast_seo field and
uses its value, if present, to alter the metatags of a page.
Because the field widget settings can not reliably be obbtained
(a widget without those settings may produce the same value) we
instead only check if the value is set. In the widget we then ensure
that no value is present if editing of title or description is
disabled.

This leads to a situtation where a custom title cna be lost if the
field widget setting is disabled and the entity is re-saved,
however, it ensures that the saved value is always equivalent to
the one displayed in the entity create/edit form.
parent c2d138a3
......@@ -176,8 +176,8 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
foreach ($values as &$value) {
$value['status'] = $value['yoast_seo']['status'];
$value['focus_keyword'] = $value['yoast_seo']['focus_keyword'];
$value['title'] = $value['yoast_seo']['edit_title'];
$value['description'] = $value['yoast_seo']['edit_description'];
$value['title'] = ($this->getSetting('edit_title') ? $value['yoast_seo']['edit_title'] : null);
$value['description'] = ($this->getSetting('edit_description') ? $value['yoast_seo']['edit_description'] : null);
}
return $values;
}
......
......@@ -3,8 +3,10 @@
namespace Drupal\yoast_seo;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Symfony\Component\Yaml\Yaml;
/**
......@@ -85,6 +87,33 @@ class SeoManager {
return $entities;
}
/**
* Returns the Real-Time SEO field of the entity.
*
* Returns the first field of the entity that is a Real-Time SEO field or
* null if none is found.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity for which to find the Real-Time SEO field.
*
* @return NULL|\Drupal\Core\Field\FieldItemListInterface
* The field item list of the field or NULL if no RTSEO field was found.
*/
public function getSeoField(FieldableEntityInterface $entity) {
$definitions = $entity->getFieldDefinitions();
// Find the first yoast_seo field on the entity.
foreach ($definitions as $definition) {
if ($definition->getType() === 'yoast_seo') {
return $entity->get($definition->getName());
}
}
// No field of yoast_seo type was found.
return NULL;
}
/**
* Get the status for a given score.
*
......
......@@ -100,43 +100,47 @@ function yoast_seo_entity_type_build(array &$entity_types) {
* If an entity has values for the custom title or description fields then we
* use those values in place of the metatags defaults.
*/
//function yoast_seo_metatags_alter(array &$metatags, array $context) {
// // Without entity there is nothing for us to do.
// if (empty($context['entity'])) {
// return;
// }
//
// /** @var \Drupal\Core\Entity\EntityInterface $entity */
// $entity = $context['entity'];
//
// /** @var \Drupal\yoast_seo\SeoManager $seo_manager */
// $seo_manager = \Drupal::service('yoast_seo.manager');
//
// $entity_type = $entity->getEntityTypeId();
// $bundle = $entity->bundle();
//
// // Abort early if we're not enabled for this entity type.
// if (!$seo_manager->isEnabledFor($entity_type, $bundle)) {
// return;
// }
//
// $field = $entity->get('field_yoast_seo')->first();
//
// // If the field has no value then we're done as well.
// if (empty($field)) {
// return;
// }
//
// $values = $field->getValue();
//
// if ($seo_manager->isEditingEnabledFor($entity_type, $bundle, 'title') &&
// !empty($values['title'])) {
// $metatags['title'] = $values['title'];
// }
//
// if ($seo_manager->isEditingEnabledFor($entity_type, $bundle, 'description') &&
// !empty($values['description'])) {
// $metatags['description'] = $values['description'];
// }
//
//}
function yoast_seo_metatags_alter(array &$metatags, array $context) {
// Without entity there is nothing for us to do.
if (empty($context['entity'])) {
return;
}
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $context['entity'];
// Make sure we have a fieldable entity or our SeoManager will produce errors.
if (!($entity instanceof \Drupal\Core\Entity\FieldableEntityInterface)) {
return;
}
/** @var \Drupal\yoast_seo\SeoManager $seo_manager */
$seo_manager = \Drupal::service('yoast_seo.manager');
$seo_field = $seo_manager->getSeoField($entity);
// Abort early if we're not enabled for this entity type.
if (is_null($seo_field)) {
return;
}
$field_item = $seo_field->first();
// If the field has no value then we're done as well.
if (empty($field_item)) {
return;
}
$values = $field_item->getValue();
// If we have a custom title, use it instead of the default one.
if (!empty($values['title'])) {
$metatags['title'] = $values['title'];
}
// If we have a custom description, use it instead of the default one.
if (!empty($values['description'])) {
$metatags['description'] = $values['description'];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment