Skip to content
Snippets Groups Projects
Commit 38d120f0 authored by David Cameron's avatar David Cameron
Browse files

Issue #2855791 by eiriksm, larowlan, dcam, mindbet: Bad link in aggregator...

Issue #2855791 by eiriksm, larowlan, dcam, mindbet: Bad link in aggregator crashes production website
parent 943ff96c
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@ use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -37,6 +38,13 @@ class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInt
*/
protected $itemStorage;
/**
* The logger service.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs an AggregatorFeedBlock object.
*
......@@ -50,11 +58,14 @@ class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInt
* The entity storage for feeds.
* @param \Drupal\aggregator\ItemStorageInterface $item_storage
* The entity storage for feed items.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* Logger factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, FeedStorageInterface $feed_storage, ItemStorageInterface $item_storage) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, FeedStorageInterface $feed_storage, ItemStorageInterface $item_storage, LoggerChannelFactoryInterface $logger_factory = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->feedStorage = $feed_storage;
$this->itemStorage = $item_storage;
$this->logger = $logger_factory->get('aggregator');
}
/**
......@@ -66,7 +77,8 @@ class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInt
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')->getStorage('aggregator_feed'),
$container->get('entity_type.manager')->getStorage('aggregator_item')
$container->get('entity_type.manager')->getStorage('aggregator_item'),
$container->get('logger.factory')
);
}
......@@ -147,11 +159,20 @@ class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInt
'#items' => [],
];
foreach ($items as $item) {
$build['list']['#items'][$item->id()] = [
'#type' => 'link',
'#url' => $item->toUrl(),
'#title' => $item->label(),
];
try {
$build['list']['#items'][$item->id()] = [
'#type' => 'link',
'#url' => $item->toUrl(),
'#title' => $item->label(),
];
}
catch (\InvalidArgumentException $exception) {
// Handle invalid item URLs.
$this->logger->notice("Received an invalid URL for aggregator item %item in feed %url", [
'%item' => $item->label(),
'%url' => $feed->getUrl(),
]);
}
}
$build['more_link'] = [
'#type' => 'more_link',
......@@ -160,6 +181,7 @@ class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInt
];
return $build;
}
return [];
}
/**
......
<?php
namespace Drupal\Tests\aggregator\Kernel\Plugin\Block;
use Drupal\aggregator\Entity\Feed;
use Drupal\aggregator\Entity\Item;
use Drupal\KernelTests\KernelTestBase;
/**
* Class AggregatorFeedBlockTest.
*
* Test that we can render the block properly.
*
* @group aggregator
*/
class AggregatorFeedBlockTest extends KernelTestBase {
public static $modules = [
'aggregator',
'block',
'options',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('aggregator_feed');
$this->installEntitySchema('aggregator_item');
}
public function testRenderBadItemUrl() {
$aggregator_feed = Feed::create([
'title' => 'testing title',
'url' => 'http://www.example.com',
]);
$aggregator_feed->save();
// Create an aggregator feed item. It has invalid URI scheme.
Item::create([
'title' => 'Never Again',
'fid' => $aggregator_feed->id(),
'link' => 'xfiles s04e13',
]);
// And another one, this time valid.
$aggregator_item = Item::create([
'title' => 'Never Again',
'fid' => $aggregator_feed->id(),
'link' => 'https://en.wikipedia.org/wiki/Never_Again_(The_X-Files)',
]);
$aggregator_item->save();
// Now try to render the block.
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = $this->container->get('plugin.manager.block');
$block = $block_manager->createInstance('aggregator_feed_block', [
'feed' => $aggregator_feed->id(),
]);
// Now when trying to build it, there should be only the item with the
// valid URL.
$build = $block->build();
$this->assertCount(1, $build["list"]["#items"]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment