diff --git a/src/EventSubscriber/FeedBlockCacheExpire.php b/src/EventSubscriber/FeedBlockCacheExpire.php index 4197dbccd70a46b72409bd29db3a129d8d8fd290..494f24c5a3cb4832a19757f5cdc30aeda131b3fd 100644 --- a/src/EventSubscriber/FeedBlockCacheExpire.php +++ b/src/EventSubscriber/FeedBlockCacheExpire.php @@ -2,19 +2,19 @@ namespace Drupal\feed_block\EventSubscriber; -use Symfony\Component\HttpKernel\Event\ResponseEvent; use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\PageCache\RequestPolicyInterface; use Drupal\Core\PageCache\ResponsePolicyInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** - * Class FeedBlockCacheExpire. + * Expires the cache correctly. * * @package Drupal\feed_block */ @@ -80,7 +80,7 @@ class FeedBlockCacheExpire implements EventSubscriberInterface { */ public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContextsManager $cache_contexts_manager, $http_response_debug_cacheability_headers = FALSE) { $this->languageManager = $language_manager; - $this->config = $config_factory->get('system.performance'); + $this->config = $config_factory->get('feed_block'); $this->requestPolicy = $request_policy; $this->responsePolicy = $response_policy; $this->cacheContextsManager = $cache_contexts_manager; @@ -101,10 +101,12 @@ class FeedBlockCacheExpire implements EventSubscriberInterface { $is_cacheable = $response instanceof CacheableResponseInterface && ($this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) && ($this->responsePolicy->check($response, $request) !== ResponsePolicyInterface::DENY); if ($is_cacheable) { + /** @var \Drupal\Core\Cache\CacheableResponseInterface $response */ $response_cacheability = $response->getCacheableMetadata(); $tags = $response_cacheability->getCacheTags(); // Only act on pages that contain a feed_block. if (in_array('feed_block', $tags)) { + /** @var \Symfony\Component\HttpFoundation\Response $response */ $this->setExpiresCacheLifetime($response); } } @@ -121,7 +123,7 @@ class FeedBlockCacheExpire implements EventSubscriberInterface { * A response object. */ protected function setExpiresCacheLifetime(Response $response) { - $cache_expiration = \Drupal::config('feed_block')->get('cache_expiration'); + $cache_expiration = $this->config->get('cache_expiration'); if (!$cache_expiration) { // Default to 1 day feed cache if not specified in settings. $cache_expiration = 86400; diff --git a/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php b/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php index 5cd594ea9350205b790c99ed67e67971851cd380..d695b98dfb0116825b94bc389db9beed83e6c832 100644 --- a/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php +++ b/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php @@ -65,10 +65,10 @@ class RSSFeedFormatter extends FormatterBase { if (!isset($instance->link)) { break; } - $elements[$delta][$inc] = array( + $elements[$delta][$inc] = [ '#theme' => 'feed_block_rss_item', '#title' => (string) $instance->title, - ); + ]; if (isset($instance->link->attributes()->href)) { // Youtube format. $elements[$delta][$inc]['#url'] = (string) $instance->link->attributes()->href; diff --git a/src/Plugin/Field/FieldWidget/RSSFeedWidget.php b/src/Plugin/Field/FieldWidget/RSSFeedWidget.php index 9432fa1804612c1a47c77ce8f58877535b4aacda..27d94f2f11b1d3a24188555a4f67af01ae91377b 100644 --- a/src/Plugin/Field/FieldWidget/RSSFeedWidget.php +++ b/src/Plugin/Field/FieldWidget/RSSFeedWidget.php @@ -2,15 +2,15 @@ namespace Drupal\feed_block\Plugin\Field\FieldWidget; +use Drupal\Core\Datetime\DateFormatterInterface; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\link\LinkItemInterface; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\link\LinkItemInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Core\Entity\EntityStorageInterface; -use Drupal\Core\Datetime\DateFormatterInterface; /** * Plugin implementation of the 'rss_feed_widget' widget. @@ -72,7 +72,7 @@ class RSSFeedWidget extends WidgetBase implements ContainerFactoryPluginInterfac $element['feed_uri'] = [ '#type' => 'url', '#title' => $this->t('Feed URL'), - '#default_value' => isset($item->feed_uri) ? $item->feed_uri : NULL, + '#default_value' => $item->feed_uri ?? NULL, '#maxlength' => 2048, '#link_type' => LinkItemInterface::LINK_EXTERNAL, '#description' => $this->t('This must be a valid RSS feed.'), @@ -81,12 +81,12 @@ class RSSFeedWidget extends WidgetBase implements ContainerFactoryPluginInterfac '#type' => 'select', '#title' => $this->t('Number of items to display'), '#options' => array_combine(range(1, 100), range(1, 100)), - '#default_value' => isset($item->count) ? $item->count : 5, + '#default_value' => $item->count ?? 5, ]; $element['display_date'] = [ '#type' => 'checkbox', '#title' => $this->t('Display date'), - '#default_value' => isset($item->display_date) ? $item->display_date : 1, + '#default_value' => $item->display_date ?? 1, ]; $date_formats = []; foreach ($this->dateStorage->loadMultiple() as $machine_name => $value) { @@ -118,13 +118,13 @@ class RSSFeedWidget extends WidgetBase implements ContainerFactoryPluginInterfac 'inverse time span' => $this->t('Time span (past dates have "-" prepended)'), 'time span' => $this->t('Time span (with "ago/hence" appended)'), ], - '#default_value' => isset($item->date_format) ? $item->date_format : 'small', + '#default_value' => $item->date_format ?? 'small', ]; $element['date']['custom_date_format'] = [ '#type' => 'textfield', '#title' => $this->t('Custom date format'), '#description' => $this->t('If "Custom", see <a href="http://us.php.net/manual/en/function.date.php" target="_blank">the PHP docs</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.'), - '#default_value' => isset($item->custom_date_format) ? $item->custom_date_format : 'F j, Y', + '#default_value' => $item->custom_date_format ?? 'F j, Y', ]; foreach ([ 'custom', @@ -147,7 +147,7 @@ class RSSFeedWidget extends WidgetBase implements ContainerFactoryPluginInterfac $element['display_description'] = [ '#type' => 'checkbox', '#title' => $this->t('Display description'), - '#default_value' => isset($item->display_description) ? $item->display_description : 1, + '#default_value' => $item->display_description ?? 1, ]; $element['description'] = [ '#type' => 'details', @@ -164,13 +164,13 @@ class RSSFeedWidget extends WidgetBase implements ContainerFactoryPluginInterfac '#title' => $this->t('Description trim length (in characters)'), '#min' => 0, '#max' => 1024, - '#default_value' => isset($item->description_length) ? $item->description_length : 255, + '#default_value' => $item->description_length ?? 255, '#description' => $this->t('For no trimming, leave this field blank (i.e, "0").'), ]; $element['description']['description_plaintext'] = [ '#type' => 'checkbox', '#title' => $this->t('Remove HTML markup from description text.'), - '#default_value' => isset($item->description_plaintext) ? $item->description_plaintext : 1, + '#default_value' => $item->description_plaintext ?? 1, ]; return $element; }