Skip to content

Instantly share code, notes, and snippets.

(function ($, Drupal) {
Drupal.behaviors.globalOpenLuciusBehavior = {
attach: function (context, settings) {
// Message toaster init.
$('.toast').toast('show');
}
};
})(jQuery, Drupal);
{#
/**
* @file
* Theme override for status messages.
*
* Displays status, error, and warning messages, grouped by type.
*
* An invisible heading identifies the messages for assistive technology.
* Sighted users see a colored box. See http://www.w3.org/TR/WCAG-TECHS/H69.html
* for info.
<?php
/**
* Implements hook_preprocess_HOOK()
*/
function YOURTHEME_preprocess_paragraph(&$variables) {
// Populate color templates vars, based on chosen color.
$paragraph = $variables['paragraph'];
if(!empty($paragraph->field_bg_color)) {
$bg_color = $paragraph->field_bg_color->color;
@jorislucius
jorislucius / MessageForm.php
Last active September 1, 2020 13:10
Multiple files upload in a custom Drupal form programmatically | Native, clean example Drupal code | Also see https://www.lucius.digital/en/blog/multiple-files-upload-custom-drupal-form-programmatically-native-clean-example-drupal-code
<?php
namespace Drupal\ol_messages\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Xss;
use Drupal\file\Entity\File;
use Drupal\ol_file\Entity\OlFile;
@jorislucius
jorislucius / MessageForm.php
Last active August 28, 2020 10:21
Rich text editor in a custom Drupal form. Also see A custom Drupal form with multiple file upload. Also see https://www.lucius.digital/en/blog/rich-text-editor-custom-drupal-form-example-code-including-data-sanitization-security
<?php
namespace Drupal\ol_messages\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\ol_messages\Services\OlMessages;
use Symfony\Component\DependencyInjection\ContainerInterface;
<?php
/**
* Render.
*
* @param $course_code
*
* @return array
* Returns a renderable array.
*/
public function render($course_code) {
<?php
/**
* Implements hook_library_info_build().
*/
function YOURMODULENAME_library_info_build() {
$libraries = [];
$module = \Drupal::moduleHandler()->getModule('YOURMODULENAME');
$module_path = $module->getPath();
$path_to_js = \Drupal::service('file_system')->realpath($module_path) . '/js';
$path_to_manifest = $path_to_js . '/build/asset-manifest.json';
@jorislucius
jorislucius / lus_channel.module.php
Last active January 15, 2018 13:02
Snippet from Drupal Lus distribution, to handle private files in channels
<?php
/**
* Implements hook_file_download().
*
* @param $uri
*
* @return array
*/
function lus_file_download($uri) {
@jorislucius
jorislucius / modulename.services.yml
Last active September 17, 2019 10:48
Yml file needed in Drupal 8 module for defining RedirectAnonymousSubscriber
services:
<yourmodulename>.event_subscriber:
class: Drupal\<yourmodulename>\EventSubscriber\RedirectAnonymousSubscriber
arguments: []
tags:
- {name: event_subscriber}
@jorislucius
jorislucius / RedirectAnonymousSubscriber.php
Last active April 2, 2023 19:16
Drupal 8 | Redirect all anonymous users to login page. With a few needed exceptions like /user/password
<?php
namespace Drupal\<yourmodulename>\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**