class AggregatorCategoryBlock

Same name in this branch

Provides an 'Aggregator category' block for the latest items in a category.

Plugin annotation


@Plugin(
  id = "aggregator_category_block",
  admin_label = @Translation("Aggregator category"),
  module = "aggregator",
  derivative = "Drupal\aggregator\Plugin\Derivative\AggregatorCategoryBlock"
)

Hierarchy

Expanded class hierarchy of AggregatorCategoryBlock

File

drupal/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorCategoryBlock.php, line 24
Contains \Drupal\aggregator\Plugin\Block\AggregatorCategoryBlock.

Namespace

Drupal\aggregator\Plugin\Block
View source
class AggregatorCategoryBlock extends BlockBase {

  /**
   * Overrides \Drupal\block\BlockBase::settings().
   */
  public function settings() {

    // By default, the block will contain 10 feed items.
    return array(
      'block_count' => 10,
    );
  }

  /**
   * Overrides \Drupal\block\BlockBase::access().
   */
  public function access() {

    // Only grant access to users with the 'access news feeds' permission.
    return user_access('access news feeds');
  }

  /**
   * Overrides \Drupal\block\BlockBase::blockForm().
   */
  public function blockForm($form, &$form_state) {
    $form['block_count'] = array(
      '#type' => 'select',
      '#title' => t('Number of news items in block'),
      '#default_value' => $this->configuration['block_count'],
      '#options' => drupal_map_assoc(range(2, 20)),
    );
    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() {
    $id = $this
      ->getPluginId();
    if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(
      ':cid' => $id,
    ))
      ->fetchObject()) {
      $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC', 0, $this->configuration['block_count'], array(
        ':cid' => $category->cid,
      ));
      $read_more = theme('more_link', array(
        'url' => 'aggregator/categories/' . $category->cid,
        'title' => t("View this category's recent news."),
      ));
      $items = array();
      foreach ($result as $item) {
        $items[] = theme('aggregator_block_item', array(
          'item' => $item,
        ));
      }

      // Only display the block if there are items to show.
      if (count($items) > 0) {
        return array(
          '#children' => theme('item_list', array(
            'items' => $items,
          )) . $read_more,
        );
      }
      return array();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AggregatorCategoryBlock::access public function Overrides \Drupal\block\BlockBase::access(). Overrides BlockBase::access
AggregatorCategoryBlock::blockForm public function Overrides \Drupal\block\BlockBase::blockForm(). Overrides BlockBase::blockForm
AggregatorCategoryBlock::blockSubmit public function Overrides \Drupal\block\BlockBase::blockSubmit(). Overrides BlockBase::blockSubmit
AggregatorCategoryBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
AggregatorCategoryBlock::settings public function Overrides \Drupal\block\BlockBase::settings(). Overrides BlockBase::settings
BlockBase::blockValidate public function Adds block type-specific validation for the block form.
BlockBase::form public function Implements \Drupal\block\BlockPluginInterface::form(). Overrides BlockPluginInterface::form 1
BlockBase::getConfig public function Returns the configuration data for the block plugin.
BlockBase::setConfig public function Sets a particular value in the block settings.
BlockBase::submit public function Implements \Drupal\block\BlockPluginInterface::submit(). Overrides BlockPluginInterface::submit
BlockBase::validate public function Implements \Drupal\block\BlockPluginInterface::validate(). Overrides BlockPluginInterface::validate
BlockBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId