Skip to content

Instantly share code, notes, and snippets.

View mattschaff's full-sized avatar

Matt Schaff mattschaff

View GitHub Profile
@mattschaff
mattschaff / AdminFormExample.php
Last active April 1, 2019 14:25
Drupal 8: Admin Form Example
<?php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Builds the example admin form
*/
@mattschaff
mattschaff / output-data.html.twig
Last active April 1, 2019 14:07
Drupal 8: Outputting Data with Twig
{#
/**
* The below assumes Twig Tweak and Twig Extensions are installed
*
* Also this is a great reference: https://blog.usejournal.com/getting-drupal-8-field-values-in-twig-22b80cb609bd
*/
#}
{# Drupal Block #}
{{ drupal_block('block_machine_name') }}
@mattschaff
mattschaff / OverrideViewFirstItem.php
Last active April 1, 2019 14:24
Drupal 8: Admin override of first item in a view
<?php
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
/**
* @file
* Hooks and procedural code for My Module
*
* This code should be in a *.module or *.inc file
@mattschaff
mattschaff / ExampleOpenNowFilter.php
Last active March 1, 2023 15:58
Drupal 8: Custom Views Filter by Open Now (also dependency injection in plugin)
<?php
namespace Drupal\my_module\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Driver\mysql\Connection;
@mattschaff
mattschaff / change_view_page_title.includes.php
Last active April 4, 2019 13:56
Drupal 8: Dynamically set page title of view page display
<?php
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_post_render().
*/
function my_module_views_post_render(ViewExecutable $view, &$output) {
if ($view->id() == 'view_machine_name') {
// Set title based on the user name of the current user profile.
@mattschaff
mattschaff / view_fields_role_access.includes.php
Last active April 4, 2019 13:56
Drupal 8: Access view fields based on user role
<?php
use Drupal\views\ViewExecutable;
/**
* Implements hook_views_pre_view().
*/
function my_module_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
// Control access to specific view fields based on user role.
if ($view->id() == 'view_machine_name') {
@mattschaff
mattschaff / redirect_with_warning.includes.php
Created April 3, 2019 14:10
Drupal 8: Redirect with a warning message
<?php
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Redirects with warning message
*
* When you need to redirect user upon a page load in procedural code.
* Otherwise, use the Controller response or ...
* ... $form_state->setRedirect('routing_machine_name'); return;
@mattschaff
mattschaff / checkbox_default_views.includes.php
Created April 3, 2019 14:14
Drupal 8: Set default value of checkbox field in views exposed form with AJAX
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter()
*/
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
switch ($form_id) {
case 'views_exposed_form':
@mattschaff
mattschaff / remove_view_duplicates.includes.php
Created April 3, 2019 14:43
Drupal 8: Remove duplicate records in view
<?php
/**
* Implements hook_preprocess_views_view
*/
function my_module_preprocess_views_view(&$vars){
// Only use unique IDs for view.
// Use this code for views with Better Exposed Filters.
if ($vars['view']->id() === 'view_name_bef') {
$rows = [];
@mattschaff
mattschaff / CustomAdminSettingService.php
Created April 4, 2019 13:37
Drupal 8: Custom service that gets and sets admin variables
<?php
namespace Drupal\my_module\Service;
use Drupal\Core\Database\Driver\mysql\Connection;
/**
* Service that peforms administration work for My Module
*