I have taken the reference from the views module which is now present in Drupal8 core.

Let’s created a custom module in which create a ‘simple node archive’ views style plugin when installed. As Drupal8 now ships with Symfony components the .info files are now .info.yml and coding syntax is changed but more similar to Drupal7.

name : simple node arvhive
description : Provide simple node archive style option for Views.
core : 8.x
package : Content display
type: module
dependencies:
  - views

No changes for the .module file.

/**
 * @file
 * Provides Simple Node archive style options for Views.
 */

The custom module is ready now let’s add a views style plugin.The directory structure will be ‘src/Plugin/views/style’ and create your style plugin file SimpleNodeArchive.php. Earlier we used hook_views_plugins() now it’s ViewsStyle annotation that describes the plugin.

/**
 * @file
 * Definition of Drupal\sna_blocks\Plugin\views\style\SimpleNodeArchive.
 */
 
namespace Drupal\sna_blocks\Plugin\views\style;
 
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Form\FormStateInterface;
 
/**
 * The default style plugin for summaries.
 *
 * @ingroup views_style_plugins
 *
 * @ViewsStyle(
 *   id = "sna_blocks",
 *   title = @Translation("Simple Node Archive"),
 *   help = @Translation("Displays result in archive formatted, with month and year that link to achive page."),
 *   theme = "sna_blocks_view_simplenodearchive",
 *   theme_file = "sna_blocks.theme.inc",
 *   display_types = {"normal"}
 * )
 */
class SimpleNodeArchive extends StylePluginBase {
  /**
   * Overrides Drupal\views\Plugin\Plugin::$usesOptions.
   */
  protected $usesOptions = TRUE;
 
  /**
   * Does the style plugin allows to use style plugins.
   *
   * @var bool
   */
  protected $usesRowPlugin = FALSE;
 
  /**
   * Does the style plugin support custom css class for the rows.
   *
   * @var bool
   */
  protected $usesRowClass = FALSE;
   
  /**
   * Does the style plugin support grouping of rows.
   *
   * @var bool
   */
  protected $usesGrouping = FALSE;
 
  protected function defineOptions() {
    $options = parent::defineOptions();
    // Define options.
    return $options;
  }
 
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    // Options form here.
  }
}

Create a ‘template’ directory relative to the module folder and add theme sna_blocks.theme.inc and template sna-blocks-view-simplenodearchive.html.twig files. the process is almost the same as what we used in an older version of Drupal.

sna_blocks.theme.inc –

/**
 * @file
 * Preprocessors and helper functions to make theming easier.
 */
 
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
 
/**
 * Prepares variables for view templates.
 *
 * Default template: sna-blocks-view-simplenodearchive.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
 *   - options: An array of options. Each option contains:
 *     - separator: A string to be placed between inline fields to keep them
 *       visually distinct.
 */
function template_preprocess_sna_blocks_view_simplenodearchive(&$variables) {
  $variables['archive_block'] = 'some data here.';
}

sna-blocks-view-simplenodearchive.html.twig -:

{#
/**
 * @file
 * Default theme implementation for unformatted summary links.
 *
 * Available variables:
 * - archive_block: Archive block data.
 
 *
 * @see template_preprocess_sna_blocks_view_simplenodearchive()
 *
 * @ingroup themeable
 */
#}
 
{{ archive_block }}

Suggestions are welcome.

Author

Write A Comment