Commit a332acf1 authored by chr.fritsch's avatar chr.fritsch Committed by Kingdutch

Issue #2730333 by chr.fritsch, Kingdutch: Content Analysis for Entities, not just nodes

parent d63177d7
Real-time SEO for Drupal 8
----------------------
Improve your Drupal SEO: write better content using Real-time SEO for Drupal module togheter with the Yoast SEO library.
Improve your Drupal SEO: write better content using Real-time SEO for Drupal
module together with the Yoast SEO library.
https://www.drupal.org/project/yoast_seo
HOW DOES IT WORK
......@@ -22,8 +23,8 @@ HOW TO CONFIGURE
----------------
By default, the Real-time SEO field is enabled for all the content types. You
can change this setting in Configuration > Development > Real-time SEO Admin Settings.
As of now, the Real-time SEO module only works on nodes and does not support
taxonomy.
The Real-time SEO module only works on entity types that implement
ContentEntityInterface (node, media and block_content) and on taxonomy term pages.
Make sure you have the "Real-time SEO" field under "Manage form display"
Make sure you have the "Meta tags" field under "Manage form display"
......
......@@ -9,14 +9,14 @@
* Implements hook_drush_command().
*/
function yoast_seo_drush_command() {
$items = array();
$items = [];
// Command yoastseo-prepare-uninstall
// prepares the module to be uninstalled.
$items['yoastseo-prepare-uninstall'] = array(
$items['yoastseo-prepare-uninstall'] = [
'description' => "Prepare uninstalling Real-time SEO. Remove fields and data.",
'aliases' => array('ypu'),
);
'aliases' => ['ypu'],
];
return $items;
}
......@@ -26,41 +26,32 @@ function yoast_seo_drush_command() {
*
* This enables to delete the fields from drupal so uninstalling the module
* doesn't fail.
* It does 3 things :
* - Remove Yoast SEO fields from the content view
* It does 2 things :
* - Detach Yoast SEO from all yoastseo enabled content types
* - Purge the fields list from all temporary deleted fields.
*
* @see https://www.drupal.org/node/2418659
*/
function drush_yoast_seo_yoastseo_prepare_uninstall() {
$supported_entities = [
'node' => 'Node',
'taxonomy_term' => 'Taxonomy term',
];
$yoast_seo_manager = \Drupal::service('yoast_seo.manager');
// 1) Remove field from content view.
$yoast_seo_manager->detachFieldHandlerFromContentView();
/** @var \Drupal\yoast_seo\SeoManager $yoast_seo_manager */
$yoast_seo_manager = \Drupal::service('yoast_seo.manager');
// 2) Detach Yoast SEO from all the content types it is attached to.
foreach ($supported_entities as $entity_type_id => $entity_type_label) {
$enabled_bundles = $yoast_seo_manager->getEnabledBundles($entity_type_id);
if (!empty($enabled_bundles)) {
foreach ($enabled_bundles as $bundle_id) {
echo "detach yoast_seo fields from $entity_type_id $bundle_id\n";
$yoast_seo_manager->detachYoastSeoFields($entity_type_id, $bundle_id);
}
foreach ($yoast_seo_manager->getEnabledBundles() as $entity_type_id => $bundles) {
foreach ($bundles as $bundle_id) {
echo "Detach Real-Time SEO fields for {$entity_type_id} {$bundle_id}\n";
$yoast_seo_manager->disableFor($entity_type_id, $bundle_id);
}
}
// 3) Purge field data.
do {
field_purge_batch(1000);
$properties = array(
$properties = [
'deleted' => TRUE,
'include_deleted' => TRUE,
);
];
$fields = entity_load_multiple_by_properties('field_config', $properties);
} while ($fields);
......
......@@ -45,7 +45,7 @@ class AnalysisFormHandler implements EntityHandlerInterface {
}
/**
* Ajax Callback for returning node preview to seo library.
* Ajax Callback for returning entity preview to seo library.
*
* @param array $form
* The complete form array.
......
......@@ -2,6 +2,7 @@
namespace Drupal\yoast_seo;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\Yaml\Yaml;
......@@ -57,10 +58,19 @@ class SeoManager {
* A list of available entity types as $id => $label.
*/
public function getSupportedEntityTypes() {
// @todo Should be the same than the ones supported by the metatag module.
return [
'node' => 'Node',
];
$supportedEntityTypes = [];
foreach ($this->entityTypeManager->getDefinitions() as $definition) {
if ($definition->entityClassImplements(ContentEntityInterface::class)) {
$supportedEntityTypes[$definition->id()] = $definition->getLabel();
}
}
// TODO: Could be removed when d.o/project/drupal/issues/2880149 lands.
if (\Drupal::service('module_handler')->moduleExists('taxonomy')) {
$supportedEntityTypes['taxonomy_term'] = 'Term';
}
return $supportedEntityTypes;
}
/**
......
......@@ -8,6 +8,8 @@ configure: yoast_seo.settings
dependencies:
- metatag
- node
- path
- views
test_dependencies:
- node
......@@ -79,23 +79,15 @@ function yoast_seo_theme() {
/**
* Implements hook_entity_type_build().
*
* Sets the default yoast_seo_form form handler to enabled entity types.
* Sets the default yoast_seo_form form handler to all entity types.
*
* @see \Drupal\Core\Entity\Annotation\EntityType
*/
function yoast_seo_entity_type_build(array &$entity_types) {
/* @var \Drupal\yoast_seo\SeoManager $seo_manager */
$seo_manager = \Drupal::service('yoast_seo.manager');
// Set the handler for all supported types.
$supported_types = $seo_manager->getSupportedEntityTypes();
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
foreach ($entity_types as &$entity_type) {
if (isset($supported_types[$entity_type->id()])) {
if (!$entity_type->hasHandlerClass('yoast_seo_preview_form')) {
$entity_type->setHandlerClass('yoast_seo_preview_form', AnalysisFormHandler::class);
}
if (!$entity_type->hasHandlerClass('yoast_seo_preview_form')) {
$entity_type->setHandlerClass('yoast_seo_preview_form', AnalysisFormHandler::class);
}
}
}
......@@ -115,8 +107,12 @@ function yoast_seo_entity_type_build(array &$entity_types) {
* @ingroup entity_crud
*/
function yoast_seo_entity_view_display_alter(EntityViewDisplayInterface $display, array $context) {
/* @var \Drupal\yoast_seo\SeoManager $seo_manager */
$seo_manager = \Drupal::service('yoast_seo.manager');
$supported_types = $seo_manager->getSupportedEntityTypes();
// Leave Yoast field labels out of the display.
if ($context['entity_type'] == 'node' && $context['view_mode'] == 'full') {
if (!empty($supported_types[$context['entity_type']]) && $context['view_mode'] == 'full') {
foreach ($display->getComponents() as $name => $options) {
if ($name == 'field_yoast_seo') {
$display->removeComponent($name);
......
......@@ -5,4 +5,4 @@ administer yoast seo:
use yoast seo:
title: 'Use Real-time SEO for Drupal'
description: 'Modify Realtime SEO for Drupal configuration per individual nodes.'
description: 'Modify Realtime SEO for Drupal configuration per individual entities.'
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