Commit 1415f2ad authored by webchick's avatar webchick

Issue #2020393 by dawehner, bdone, pcambra, oadaeh: Convert "Recent content" block to a View.

parent 672295bb
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\node\Plugin\Block\RecentContentBlock.
*/
namespace Drupal\node\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'Recent content' block.
*
* @Block(
* id = "node_recent_block",
* admin_label = @Translation("Recent content"),
* category = @Translation("Lists (Views)")
* )
*/
class RecentContentBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'block_count' => 10,
);
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account) {
return $account->hasPermission('access content');
}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, &$form_state) {
$form['block_count'] = array(
'#type' => 'select',
'#title' => t('Number of recent content items to display'),
'#default_value' => $this->configuration['block_count'],
'#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
);
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['block_count'] = $form_state['values']['block_count'];
}
/**
* {@inheritdoc}
*/
public function build() {
if ($nodes = node_get_recent($this->configuration['block_count'])) {
return array(
'#theme' => 'node_recent_block',
'#nodes' => $nodes,
);
}
else {
return array(
'#children' => t('No content available.'),
);
}
}
}
......@@ -31,7 +31,7 @@ class NodeBlockFunctionalTest extends NodeTestBase {
*
* @var array
*/
public static $modules = array('block');
public static $modules = array('block', 'views');
public static function getInfo() {
return array(
......@@ -61,7 +61,7 @@ public function testRecentNodeBlock() {
));
// Enable the recent content block with two items.
$block = $this->drupalPlaceBlock('node_recent_block', array('id' => 'test_block', 'block_count' => 2));
$block = $this->drupalPlaceBlock('views_block:content_recent-block_1', array('id' => 'test_block', 'items_per_page' => 2));
// Test that block is not visible without nodes.
$this->drupalGet('');
......@@ -100,13 +100,13 @@ public function testRecentNodeBlock() {
$this->assertText($node3->label(), 'Node found in block.');
// Check to make sure nodes are in the right order.
$this->assertTrue($this->xpath('//div[@id="block-test-block"]/div/table/tbody/tr[position() = 1]/td/div/a[text() = "' . $node3->label() . '"]'), 'Nodes were ordered correctly in block.');
$this->assertTrue($this->xpath('//div[@id="block-test-block"]//table/tbody/tr[position() = 1]/td/a[text() = "' . $node3->label() . '"]'), 'Nodes were ordered correctly in block.');
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
// Set the number of recent nodes to show to 10.
$block->getPlugin()->setConfigurationValue('block_count', 10);
$block->getPlugin()->setConfigurationValue('items_per_page', 10);
$block->save();
// Post an additional node.
......
......@@ -160,12 +160,6 @@ function node_theme() {
'variables' => array('node' => NULL),
'file' => 'node.pages.inc',
),
'node_recent_block' => array(
'variables' => array('nodes' => NULL),
),
'node_recent_content' => array(
'variables' => array('node' => NULL),
),
'node_edit_form' => array(
'render element' => 'form',
'template' => 'node-edit-form',
......@@ -606,9 +600,6 @@ function node_preprocess_block(&$variables) {
case 'node_syndicate_block':
$variables['attributes']['role'] = 'complementary';
break;
case 'node_recent_block':
$variables['attributes']['role'] = 'navigation';
break;
}
}
}
......@@ -1097,94 +1088,6 @@ function node_get_recent($number = 10) {
return $nodes ? $nodes : array();
}
/**
* Returns HTML for a list of recent content.
*
* @param $variables
* An associative array containing:
* - nodes: An array of recent node entities.
*
* @ingroup themeable
*/
function theme_node_recent_block($variables) {
$rows = array();
$output = '';
$l_options = array('query' => drupal_get_destination());
foreach ($variables['nodes'] as $node) {
$row = array();
$node_recent_content = array(
'#theme' => 'node_recent_content',
'#node' => $node,
);
$row[] = array(
'data' => drupal_render($node_recent_content),
'class' => 'title-author',
);
if ($node->access('update')) {
$row[] = array(
'data' => l(t('edit'), 'node/' . $node->id() . '/edit', $l_options),
'class' => 'edit',
);
}
if ($node->access('delete')) {
$row[] = array(
'data' => l(t('delete'), 'node/' . $node->id() . '/delete', $l_options),
'class' => 'delete',
);
}
$rows[] = $row;
}
if ($rows) {
$table = array(
'#theme' => 'table',
'#rows' => $rows,
);
$output = drupal_render($table);
if (user_access('access content overview')) {
$more_link = array(
'#theme' => 'more_link',
'#url' => 'admin/content',
'#title' => t('Show more content'),
);
$output .= drupal_render($more_link);
}
}
return $output;
}
/**
* Returns HTML for a recent node to be displayed in the recent content block.
*
* @param $variable
* An associative array containing:
* - node: A node entity.
*
* @ingroup themeable
*/
function theme_node_recent_content($variables) {
$node = $variables['node'];
$output = '<div class="node-title">';
$output .= l($node->label(), 'node/' . $node->id());
$mark = array(
'#theme' => 'mark',
'#status' => node_mark($node->id(), $node->getChangedTime()),
);
$output .= drupal_render($mark);
$output .= '</div><div class="node-author">';
$username = array(
'#theme' => 'username',
'#account' => $node->getAuthor(),
);
$output .= drupal_render($username);
$output .= '</div>';
return $output;
}
/**
* Implements hook_form_FORM_ID_alter() for block_form().
*
......
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