Commit 877a529b authored by Kingdutch's avatar Kingdutch

Issue #3110455 by Kingdutch, Znak, jibran, c_archer: Metatags depending on URL...

Issue #3110455 by Kingdutch, Znak, jibran, c_archer: Metatags depending on URL cause errors for unsaved entities after updating to Drupal 8.8.0
parent 17fc964f
......@@ -2,18 +2,10 @@
namespace Drupal\yoast_seo;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\RendererInterface;
use Drupal\metatag\MetatagManagerInterface;
use Drupal\user\Entity\User;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
/**
......@@ -23,9 +15,32 @@ use Symfony\Component\Routing\RouterInterface;
*/
class EntityAnalyser {
/**
* De Drupal entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Drupal content renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The metatag manager.
*
* @var \Drupal\metatag\MetatagManagerInterface
*/
protected $metatagManager;
/**
* The Drupal router.
*
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $router;
/**
......@@ -101,14 +116,10 @@ class EntityAnalyser {
}
}
// Add some other fields.
// Add fields that our widget displays.
$data['title'] = $entity->label();
$data['url'] = '';
// An entity must be saved before it has a URL.
if (!$entity->isNew()) {
$data['url'] = $entity->toUrl()->toString();
}
$data['url'] = !$entity->isNew() ? $entity->toUrl()->toString() : '';
// Add our HTML as analyzable text (Yoast will sanitize).
$data['text'] = $html->__toString();
......@@ -148,6 +159,10 @@ class EntityAnalyser {
protected function replaceContextAwareTokens(array &$metatags, EntityInterface $entity) {
foreach ($metatags as $tag => $value) {
$metatags[$tag] = str_replace('[current-page:title]', $entity->getTitle(), $value);
// URL metatags cause issues for new nodes as they don't have a URL yet.
if ($entity->isNew() && (substr($tag, -4) === '_url')) {
$metatags[$tag] = '';
}
}
}
......
<?php
namespace Drupal\Tests\yoast_seo\Kernel;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\yoast_seo\Traits\YoastSEOTestTrait;
/**
* Tests the entity analyzer.
*
* @group yoast_seo
*/
class EntityAnalyserTest extends KernelTestBase {
use YoastSEOTestTrait;
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
use NodeCreationTrait {
createNode as drupalCreateNode;
}
/**
* The Real Time SEO Entity Analyzer.
*
* @var \Drupal\yoast_seo\EntityAnalyser
*/
protected $entityAnalyzer;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'yoast_seo',
'node',
'datetime',
'user',
'system',
'filter',
'field',
'text',
'token',
'metatag',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Set up everything needed to be able to use nodes.
$this->installSchema('system', 'sequences');
$this->installSchema('node', 'node_access');
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('date_format');
$this->installConfig('filter');
$this->installConfig('node');
// Install the default metatag configuration which sets some presets for
// tokens that affect how rendering happens.
$this->installConfig('metatag');
// Create an article content type that we will use for testing.
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
'display_submitted' => FALSE,
]);
DateFormat::create([
'id' => 'fallback',
'label' => 'Fallback',
'pattern' => 'Y-m-d',
])->save();
// Add our test field to the node type.
$this->createYoastSeoField('node', 'article', 'field_seo', 'SEO');
$this->entityAnalyzer = $this->container->get('yoast_seo.entity_analyser');
}
/**
* Tests that the entity preview works with unsaved nodes.
*/
public function testEntityPreviewWithUnsavedNode() {
// Can't use createNode because it saves the node, which we don't want.
$unsaved_node = Node::create([
'type' => 'article',
'title' => $this->randomMachineName(8),
'body' => [
[
'value' => $this->randomMachineName(32),
'format' => filter_default_format(),
],
],
'uid' => 0,
]);
$preview_data = $this->entityAnalyzer->createEntityPreview($unsaved_node);
$this->assertNotEmpty($preview_data['title']);
$this->assertNotEmpty($preview_data['text']);
$this->assertEmpty($preview_data['url']);
}
/**
* Tests that the entity preview works with saved nodes.
*/
public function testEntityPreviewWithSavedNode() {
$node = $this->drupalCreateNode([
'type' => 'article',
]);
$preview_data = $this->entityAnalyzer->createEntityPreview($node);
$this->assertNotEmpty($preview_data['title']);
$this->assertNotEmpty($preview_data['text']);
$this->assertNotEmpty($preview_data['url']);
}
}
<?php
namespace Drupal\Tests\yoast_seo\Traits;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Provides common functionality for the Yoast SEO test classes.
*/
trait YoastSEOTestTrait {
/**
* Creates a field of a yoast seo field storage on the specified bundle.
*
* @param string $entity_type
* The type of entity the field will be attached to.
* @param string $bundle
* The bundle name of the entity the field will be attached to.
* @param string $field_name
* The name of the field; if it already exists, a new instance of the
* existing field will be created.
* @param string $field_label
* The label of the field.
* @param int $cardinality
* The cardinality of the field.
*/
protected function createYoastSeoField($entity_type, $bundle, $field_name, $field_label, $cardinality = 1) {
// Look for or add the specified field to the requested entity bundle.
if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
FieldStorageConfig::create([
'field_name' => $field_name,
'type' => 'yoast_seo',
'entity_type' => $entity_type,
'cardinality' => $cardinality,
'settings' => [],
])->save();
}
if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $field_label,
'settings' => [],
])->save();
}
}
}
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