Commit 328ed12e authored by vagrant's avatar vagrant

Issue #2656210: Replace listings with views

parent 06ece933
......@@ -4,3 +4,5 @@ core: 8.x
type: module
dependencies:
- node
- views
......@@ -11,11 +11,11 @@ use \Drupal\Core\Url;
* Implements hook_ENTITY_TYPE_view().
*/
function blog_user_view(&$build, $account, $display, $view_mode) {
if ($account->hasPermission('create blog content')) {
if ($account->hasPermission('create blog_post content')) {
// Build internal link based on router.
$url = Url::fromRoute('blog.user', array('user' => $account->id()));
$url = Url::fromRoute('view.blog.blog_user_all', array('arg_0' => $account->id()));
$internal_link = \Drupal::l(t('View recent blog entries'), $url);
//TODO review whether this is still an acceptable way to add to the build array.
$build['summary']['blog'] = array(
'#type' => 'user_profile_item',
......@@ -80,19 +80,19 @@ function blog_menu_local_tasks_alter(&$data, $route_name) {
$user = \Drupal::routeMatch()->getParameter('user');
// Add action link to 'node/add/blog' on 'blog' page.
if ($route_name == 'blog.blog') {
if (\Drupal::service('path.validator')->isValid('node/add/blog')) {
if ($route_name == 'view.blog.blog_user_all') {
if (\Drupal::service('path.validator')->isValid('node/add/blog_post')) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => t('Create new blog entry'),
'href' => 'node/add/blog',
),
'title' => t('Create new blog post'),
'href' => 'node/add/blog',
),
);
}
}
// Provide a helper action link to the author on the 'blog/%' page.
elseif ($route_name == 'blog.user' && !empty($user) && $account->id() == $user->id()) {
elseif ($route_name == 'view.blog.blog_user_all' && !empty($user) && $account->id() == $user->id()) {
$data['actions']['output']['blog'] = array(
'#theme' => 'menu_local_action',
);
......
blog.blog:
path: '/blog'
defaults:
_controller: '\Drupal\blog\Controller\BlogController::latestBlogPosts'
_title: 'Blogs'
requirements:
_permission: 'access content'
blog.blog_rss:
path: '/blog/feed'
defaults:
_controller: '\Drupal\blog\Controller\BlogController::latestBlogPostsRss'
requirements:
_permission: 'access content'
blog.user:
path: '/blog/{user}'
defaults:
_controller: '\Drupal\blog\Controller\BlogController::userBlogPosts'
_title_callback: '\Drupal\blog\Controller\BlogController::userBlogTitle'
requirements:
_permission: 'access content'
blog.user_rss:
path: '/blog/{user}/feed'
defaults:
_controller: '\Drupal\blog\Controller\BlogController::userBlogPostsRss'
requirements:
_permission: 'access content'
uuid: 6e29822e-3926-44a1-a034-527d07fe0991
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.node.teaser
- node.type.blog_post
module:
- node
- user
id: blog
label: Blog
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' Previous'
next: 'Next ›'
first: '« First'
last: 'Last »'
quantity: 9
style:
type: default
row:
type: 'entity:node'
options:
relationship: none
view_mode: teaser
fields:
title:
id: title
table: node_field_data
field: title
entity_type: node
entity_field: title
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
settings:
link_to_entity: true
plugin_id: field
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
click_sort_column: value
type: string
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
filters:
status:
value: true
table: node_field_data
field: status
plugin_id: boolean
entity_type: node
entity_field: status
id: status
expose:
operator: ''
group: 1
type:
id: type
table: node_field_data
field: type
value:
blog_post: blog_post
entity_type: node
entity_field: type
plugin_id: bundle
sorts:
created:
id: created
table: node_field_data
field: created
order: DESC
entity_type: node
entity_field: created
plugin_id: date
relationship: none
group_type: group
admin_label: ''
exposed: false
expose:
label: ''
granularity: second
title: 'Blog posts'
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
blog_all:
display_plugin: page
id: blog_all
display_title: 'All blog posts'
position: 1
display_options:
display_extenders: { }
path: blog
display_description: ''
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
blog_block:
display_plugin: block
id: blog_block
display_title: Block
position: 5
display_options:
display_extenders: { }
title: 'Recent blog posts'
defaults:
title: false
style: false
row: false
style:
type: default
options: { }
row:
type: fields
options:
default_field_elements: true
inline: { }
separator: ''
hide_empty: false
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
blog_user_all:
display_plugin: page
id: blog_user_all
display_title: 'All user blog posts'
position: 1
display_options:
display_extenders: { }
path: blog/%
display_description: ''
arguments:
uid:
id: uid
table: node_field_data
field: uid
relationship: none
group_type: group
admin_label: ''
default_action: 'not found'
exception:
value: all
title_enable: false
title: All
title_enable: true
title: '{{ arguments.uid }}''s Blog'
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
entity_type: node
entity_field: uid
plugin_id: numeric
defaults:
arguments: false
relationships: false
relationships: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
feed_1:
display_plugin: feed
id: feed_1
display_title: 'All blog posts feed'
position: 2
display_options:
display_extenders: { }
row:
type: node_rss
pager:
type: some
style:
type: rss
path: blog/feed
displays:
default: default
page_1: page_1
display_description: ''
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'user.node_grants:view'
- user.permissions
tags: { }
feed_2:
display_plugin: feed
id: feed_2
display_title: 'All user blog posts feed'
position: 2
display_options:
display_extenders: { }
row:
type: node_rss
pager:
type: some
style:
type: rss
path: blog/feed
displays:
default: default
page_1: page_1
display_description: ''
arguments:
uid:
id: uid
table: node_field_data
field: uid
relationship: none
group_type: group
admin_label: ''
default_action: 'not found'
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
entity_type: node
entity_field: uid
plugin_id: numeric
defaults:
arguments: false
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- 'user.node_grants:view'
- user.permissions
tags: { }
......@@ -32,143 +32,13 @@ class BlogLister implements BlogListerInterface {
protected $account;
/**
* Constructs a BookManager object.
* Constructs a BlogLister object.
*/
public function __construct(AccountInterface $account, ConfigFactoryInterface $config_factory) {
$this->account = $account;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public function allBlogPosts() {
$build = array();
$query = db_select('node_field_data', 'n');
$query->addTag('node_access');
$query->condition('type','blog');
$query->condition('status',1);
$count_query = clone $query;
$count_query->addExpression('Count(n.nid)');
$paged_query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$paged_query->limit($this->configFactory->get('node.settings')->get('items_per_page'));
$paged_query->setCountQuery($count_query);
$nids = $paged_query
->fields('n', array('nid', 'sticky', 'created'))
->orderBy('sticky', 'DESC')
->orderBy('created', 'DESC')
->execute()
->fetchCol();
if (!empty($nids)) {
$nodes = node_load_multiple($nids);
$build['nodes'] = node_view_multiple($nodes);
$build['pager'] = array(
'#theme' => 'pager',
'#weight' => 5,
);
$build['attached']['feed'][] = array('/blog/feed', t('RSS - blogs'));
}
else {
drupal_set_message(t('No blog entries have been created.'));
}
return $build;
}
/**
* {@inheritdoc}
*/
public function allBlogPostsRss() {
$nids = db_select('node_field_data', 'n')
->fields('n', array('nid', 'created'))
->condition('type', 'blog')
->condition('status', 1)
->orderBy('created', 'DESC')
->range(0,$this->configFactory->get('node.settings')->get('items_per_page'))
->addTag('node_access')
->execute()
->fetchCol();
$channel['title'] = t('!site_name blogs', array('!site_name' => $this->configFactory->get('system.site')->get('name')));
//TODO _url is a deprecated replacement for url(), need to update with urls derived from routes.
$channel['link'] = _url('blog', array('absolute' => TRUE));
return node_feed($nids, $channel);
}
/**
* {@inheritdoc}
*/
public function userBlogPosts(UserInterface $user) {
$build = array();
$query = db_select('node_field_data', 'n');
$query->addTag('node_access');
$query->condition('type','blog');
$query->condition('status',1);
$query->condition('uid', $user->id());
$count_query = clone $query;
$count_query->addExpression('Count(n.nid)');
$paged_query = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$paged_query->limit($this->configFactory->get('node.settings')->get('items_per_page'));
$paged_query->setCountQuery($count_query);
$nids = $paged_query
->fields('n', array('nid', 'sticky', 'created'))
->condition('type', 'blog')
->orderBy('sticky', 'DESC')
->orderBy('created', 'DESC')
->execute()
->fetchCol();
if (!empty($nids)) {
$nodes = node_load_multiple($nids);
$build['nodes']= node_view_multiple($nodes);
$build['pager'] = array(
'#theme' => 'pager',
'#weight' => 5,
);
$build['#attached']['feed'][] = array('/blog/' . $user->id() . '/feed', t('RSS - !title', array('!title' => $user->getUsername() . t("'s blog"))));
}
else {
if ($this->account->id() == $user->id()) {
drupal_set_message(t('You have not created any blog entries.'));
}
else {
drupal_set_message(t('!author has not created any blog entries.', array('!author' => $user->getUsername())));
}
}
return $build;
}
/**
* {@inheritdoc}
* @TODO fix this, it breaks since the removal of node_feed.
* going to have to convert this to views with RSS it seems.
*/
public function userBlogPostsRss(UserInterface $user) {
$nids = db_select('node_field_data', 'n')
->fields('n', array('nid', 'created'))
->condition('type', 'blog')
->condition('uid', $user->id())
->condition('status', 1)
->orderBy('created', 'DESC')
->range(0,$this->configFactory->get('node.settings')->get('items_per_page'))
->addTag('node_access')
->execute()
->fetchCol();
$channel['title'] = t("!name's blog", array('!name' => $user->getUsername()));
//TODO _url is a deprecated replacement for url(), need to update with urls derived from routes.
$channel['link'] = _url('blog/' . $user->id(), array('absolute' => TRUE));
return node_feed($nids, $channel);
}
/**
* {@inheritdoc}
*
......
......@@ -14,32 +14,6 @@ use Drupal\user\UserInterface;
*/
interface BlogListerInterface {
/**
* Constructs listing page for all blog posts.
*
*/
public function allBlogPosts();
/**
* Constructs listing page for user blog posts
*
* @param UserInterface $user
*/
public function userBlogPosts(UserInterface $user);
/**
* Constructs RSS feed for all blog posts.
*
*/
public function allBlogPostsRss();
/**
* Constructs RSS feed for user blog posts
*
* @param UserInterface $user
*/
public function userBlogPostsRss(UserInterface $user);
/**
* Returns a title for a user blog
*
......
......@@ -16,7 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
*
* Controller routines for blog routes.
* Controller routines for blog.
*/
class BlogController implements ContainerInjectionInterface {
......@@ -46,50 +46,6 @@ class BlogController implements ContainerInjectionInterface {
);
}
/**
* Prints a listing of all blog posts.
*
* @return array
* A render array representing the listing of all blog content.
*/
public function latestBlogPosts() {
return $this->blogLister->allBlogPosts();
}
/**
* Outputs a blog RSS feed.
*
* @return array
* A render array representing the listing of all blog content.
*/
public function latestBlogPostsRss() {
return $this->blogLister->allBlogPostsRss();
}
/**
* Prints a listing of user blog posts.
*
* @param UserInterface $user
*
* @return array
* A render array representing the listing of all user blog posts.
*/
public function userBlogPosts(UserInterface $user) {
return $this->blogLister->userBlogPosts($user);
}
/**
* Outputs a user blog RSS feed.
*
* @param UserInterface $user
*
* @return array
* A render array representing the listing of all user blog postsi.
*/
public function userBlogPostsRss(UserInterface $user) {
return $this->blogLister->userBlogPostsRss($user);
}
/**
* Returns a title for user blog pages
*
......
<?php
/**
* @file
* Contains \Drupal\blog\Plugin\Block\BlogBlock .
*/
namespace Drupal\blog\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a latest blog posts block.
*
* @Block(
* id = "blog_block",
* admin_label = @Translation("Recent blog posts"),
* category = @Translation("Blog")
* )
*/
class BlogBlock extends BlockBase {
/**
* Overrides \Drupal\block\BlockBase::defaultConfiguration().
*/
public function defaultConfiguration() {
return array(
'blog_block_count' => 10,
);
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {