Commit ea05fde7 authored by Chris Shantz's avatar Chris Shantz
Browse files

Merge branch '1.0.x' into prod/1.0.x

parents 2df8c03a c99cdcd3
......@@ -38,15 +38,14 @@ process:
no_stub: true
field_author: field_author/0/title
field_uw_blog_summary/value:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_blog_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
......
......@@ -51,15 +51,14 @@ process:
source: field_event_image/0/fid
no_stub: true
field_uw_event_summary/value:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_event_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
......
......@@ -32,15 +32,14 @@ process:
source: field_news_image/0/fid
no_stub: true
field_uw_news_summary/value:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_news_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
......
......@@ -35,15 +35,14 @@ process:
source: field_profile_type
no_stub: true
field_uw_profile_summary/value:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_profile_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
......
......@@ -28,8 +28,8 @@ process:
source: field_event_image/0/fid
no_stub: true
field_uw_meta_description:
plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
langcode:
plugin: default_value
......
<?php
namespace Drupal\uw_migrate\Plugin\migrate\process;
use Drupal\Component\Utility\Html;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Custom process plugin to prepare the summary value from original body.
*
* @code
* process:
* type:
* plugin: uw_summary
* trimmed: true
* max_length: 200
* source: body
* @endcode
*
* @MigrateProcessPlugin(
* id = "uw_summary",
* handle_multiples = TRUE
* )
*/
class UwSummary extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$new_value = $value[0] ?? $value;
$trimmed = $this->configuration['trimmed'] ?? FALSE;
// Define settings based on the type of summary.
$format = 'uw_tf_standard';
$max_length = NULL;
if ($trimmed) {
$format = 'plain_text';
$max_length = $this->configuration['max_length'] ?? 160;
}
if (isset($new_value['summary']) && (string) $new_value['summary'] !== '') {
$summary = $new_value['summary'];
}
else {
$summary = $new_value['value'] ?? '';
}
// Remove extra spaces, if there are any.
$summary = trim($summary);
$filter_types_to_skip = [];
if ($trimmed) {
// Remove "filter_autop" filter to not append extra <p> tags.
$filter_types_to_skip[] = 'filter_autop';
}
// Strip tags because trimmed version should not have any HTML markup.
// Temporary replace <!--break--> string with custom tokens to preserve
// teaser breaks (HTML comments are always stripped by default).
// We replace it back after check_markup() call, because it can be stripped
// there as well.
$summary = str_replace('<!--break-->', '[uw-teaser-break]', $summary);
if ($trimmed) {
$summary = strip_tags($summary);
}
// Process text via configured text format filters to make sure the value
// is safe and valid.
$summary = (string) check_markup($summary, $format, '', $filter_types_to_skip);
$summary = str_replace('[uw-teaser-break]', '<!--break-->', $summary);
// Now retrieve the actual summary, trimmed if necessary.
$summary = text_summary($summary, $format, $max_length);
// Normalize HTML to fix unclosed tags.
// Usually, it's done by text_summary(), but it exits earlier if the teaser
// break is presented.
$summary = Html::normalize($summary);
return $summary;
}
}
......@@ -30,8 +30,7 @@ class UwNode extends Node {
*/
public function prepareRow(Row $row) {
if ($this->configuration['include_page_width']) {
$nid = $row->getSourceProperty('nid');
$row->setSourceProperty('wide_page_width', $this->getPageSettings($nid));
$row->setSourceProperty('wide_page_width', $this->getPageSettings($row));
}
return parent::prepareRow($row);
}
......@@ -39,15 +38,16 @@ class UwNode extends Node {
/**
* Loads node page settings from "uw_page_settings_node" table.
*
* @param $nid
* Node ID.
* @param \Drupal\migrate\Row $row
* The source row.
*/
protected function getPageSettings($nid) {
protected function getPageSettings(Row $row) {
$nid = $row->getSourceProperty('nid');
$vid = $row->getSourceProperty('vid');
$result = $this->select('uw_page_settings_node', 'ps')
->fields('ps')
->condition('nid', $nid)
->orderBy('ps.vid', 'DESC')
->range(0, 1)
->condition('vid', $vid)
->execute()
->fetchField(2);
return isset($result) ? (int) $result : 0;
......
......@@ -40,7 +40,7 @@ class UwContentParser {
// Append delimiter if requested.
if ($include_delimiters) {
$sections[] = $delimiter;
$sections[] = $this->clean($delimiter, FALSE);
}
// Continue with the remaining content.
......@@ -286,20 +286,25 @@ class UwContentParser {
/**
* Removes HTML tags and whitespaces from HTML snippet.
*/
protected function clean($content) {
// Remove special characters.
protected function clean($content, $strip_tags = TRUE) {
// Replace a commonly used non-breaking space: &nbsp.
$content = str_replace('&nbsp;', ' ', $content);
// Remove other special characters.
$content = preg_replace("/&#?[a-z0-9]{2,8};/i", '', $content);
$allowed_tags = array_merge([
'a', 'img', 'iframe'
], array_keys($this->getTagsMapping()));
if ($strip_tags) {
$allowed_tags = array_merge([
'a', 'img', 'iframe'
], array_keys($this->getTagsMapping()));
// Allow embedding tags.
$allowed_tags_list = array_map(function ($item) {
return "<$item>";
}, $allowed_tags);
// Allow embedding tags.
$allowed_tags_list = array_map(function ($item) {
return "<$item>";
}, $allowed_tags);
return trim(strip_tags($content, implode('', $allowed_tags_list)));
$content = strip_tags($content, implode('', $allowed_tags_list));
}
return trim($content);
}
/**
......
<?php
namespace Drupal\Tests\uw_migrate\Kernel\process;
use Drupal\KernelTests\KernelTestBase;
use Drupal\uw_migrate\Plugin\migrate\process\UwSummary;
/**
* Tests the uw_taxonomy_term_lookup custom plugin.
*
* @coversDefaultClass \Drupal\uw_migrate\Plugin\migrate\process\UwSummary
* @group uw_migrate
*/
class UwSummaryTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected $strictConfigSchema = FALSE;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system', 'migrate', 'text', 'filter', 'file', 'media', 'simplify_menu',
'editor', 'linkit', 'ckeditor', 'token', 'media_embed_extra', 'node',
'uw_cfg_common', 'taxonomy', 'user',
];
/**
* The source row.
*
* @var \Drupal\migrate\Row
*/
protected $row;
/**
* The migrate mock object.
*
* @var \Drupal\migrate\MigrateExecutable|\PHPUnit\Framework\MockObject\MockObject
*/
protected $migrateExecutable;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('filter');
$this->installConfig('text');
// Install uw_cfg_common to create "uw_tf_standard" text format.
$this->installConfig('uw_cfg_common');
$this->row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
->disableOriginalConstructor()
->getMock();
}
/**
* Lookup for the existing term by name.
*
* @covers ::transform
* @dataProvider providerTransform
*/
public function testTransform($source, $original, $trimmed) {
$plugin = new UwSummary(['trimmed' => FALSE], 'uw_summary', []);
$actual = $plugin->transform($source, $this->migrateExecutable, $this->row, 'destination');
$this->assertSame($original, $actual);
$plugin_trimmed = new UwSummary(['trimmed' => TRUE], 'uw_summary', []);
$actual_trimmed = $plugin_trimmed->transform($source, $this->migrateExecutable, $this->row, 'destination');
$this->assertSame($trimmed, $actual_trimmed);
}
/**
* Provider for self::testTransform().
*/
public function providerTransform() {
return [
'One line of text.' => [
[
'value' => '<p>Body text.</p>',
'summary' => '<p>Summary text.</p>'
],
'<p>Summary text.</p>',
'Summary text.',
],
'Multiple lines with some extra markup.' => [
[
'value' => '<h2>Stuff</h2>
<p>The stuff is <a href=https://google.ca>found here</a>.</p>
<p>It is really great stuff and here I am describing it for what will end up being more than 160 characters, at least I hope &ndash; it&rsquo;s not like I&rsquo;m keeping track. (enough characters to take us to say, 595 characters), and so on.</p>',
'summary' => '',
],
"<h2>Stuff</h2>
<p>The stuff is <a href=\"https://google.ca\">found here</a>.</p>
<p>It is really great stuff and here I am describing it for what will end up being more than 160 characters, at least I hope – it’s not like I’m keeping track. (enough characters to take us to say, 595 characters), and so on.</p>",
'Stuff
The stuff is found here.',
],
'Multiple lines with teaser break.' => [
[
'value' => '<h2>Stuff</h2>
<p>The stuff is <a href=https://google.ca>found here</a>.<!--break--></p>Some other text.',
],
'<h2>Stuff</h2>
<p>The stuff is <a href="https://google.ca">found here</a>.</p>',
'Stuff
The stuff is found here.',
]
];
}
}
......@@ -4,7 +4,6 @@ type: module
core_version_requirement: ^8 || ^9
dependencies:
- drupal:migrate_drupal
- drupal:views
- migrate_plus:migrate_plus
- migrate_scanner:migrate_scanner
- migrate_tools:migrate_tools
......
......@@ -5,13 +5,11 @@
* UW migrate module.
*/
use Drupal\Component\Utility\Html;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Row;
use Drupal\uw_migrate\Plugin\migrate\destination\UwEntityContentBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
/**
* Implements hook_migrate_destination_info_plugins_alter().
......@@ -149,18 +147,3 @@ function uw_migrate_empty($value) {
function uw_migrate_get_component_delta(array $value) {
return $value[0] ? $value[1] : NULL;
}
/**
* Strips HTML tags and trims "Body" text to ~150 characters.
*/
function uw_migrate_summary($value) {
$summary = $value['summary'] ?? '';
if (empty($summary)) {
$summary = FieldPluginBase::trimText([
'max_length' => 150,
'word_boundary' => TRUE,
'ellipsis' => TRUE,
], Html::decodeEntities(trim(strip_tags($value['value']))));
}
return $summary;
}
Supports Markdown
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