Skip to content

Instantly share code, notes, and snippets.

View MatthieuScarset's full-sized avatar
🤷‍♂️
Probably me

MattGyver MatthieuScarset

🤷‍♂️
Probably me
View GitHub Profile
@MatthieuScarset
MatthieuScarset / mymodule.install
Created April 16, 2024 15:41
Drupal - Hook update to copy values from one field to another - runs after config import
<?php
// drush updb ; trigger expection so the update don't pass.
// drush cim ; import your new field.
// drush updb ; the hook update is successfull and values are copied.
/**
* Populate formatted texts from previous field on paragraph callout.
*/
function mymodule_update_10103(&$sandbox): void {
@MatthieuScarset
MatthieuScarset / field_group_delete.php
Created September 19, 2023 13:20
Drupal - Programmatically delete field_group from form and view displays
<?php
$entity_type = 'node';
$group_name = 'the_group_id';
$context = 'form';
$form_displays = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->loadByProperties(['targetEntityType' => 'node']);
foreach ($form_displays as $display) {
@MatthieuScarset
MatthieuScarset / composer.json
Created August 29, 2023 11:56
PHPCS - One line format file (composer custom script)
// Format/fix uncommitted files
"scripts": {
"codefix": "for F in $(git diff --name-only); do vendor/bin/phpcbf \"$F\"; done",
}
// ...
@MatthieuScarset
MatthieuScarset / MyCustomForm.php
Last active November 11, 2023 20:24
Drupal - Autosubmit exposed form in JS
<?php
namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a custom form.
*/
@MatthieuScarset
MatthieuScarset / get_view_result_values.php
Created April 24, 2023 21:55
Drupal - Get values of a View result row.
<?php
/**
* Get the value out of a view Result for a given date field.
*
* @param \Drupal\views\ResultRow $result
* A given view result.
* @param \Drupal\views\Plugin\views\field\EntityField $field
* A given date field.
*
@MatthieuScarset
MatthieuScarset / gist:1c4d1c5230be882c68ac55e377978e69
Created November 25, 2022 14:20
Drupal - Custom view page title
<?php
// Custom view page title
// @see https://www.drupal.org/node/2067859#comment-14675179
/**
* Implements hook_views_post_render().
*/
function mymodule_views_post_render(Drupal\views\ViewExecutable $view) {
if ($view->element['#view_id'] == 'id_of_view') {
@MatthieuScarset
MatthieuScarset / drupal_get_title_from_url_module.php
Last active November 25, 2022 14:24
Get the page title from a Url in Drupal 8+
<?php
/**
* @file
* Contains a custom function to get the title from a Url.
*/
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
@MatthieuScarset
MatthieuScarset / fullclick.scss
Last active July 17, 2023 14:56
Make HTML element fully clickable (e.g. card <div> with a link)
/// Make element fully-clickable.
/// @author Matthieu Scarset
/// @param string $element
/// (optional) The button element which should get the click. Default: 'a'.
/// @param string $wrapper
/// (optional) The element's HTML wrapper(s), useful if convoluted markup. Default: empty string.
/// @link https://gist.github.com/MatthieuScarset/397da3b000ce6d9523dd50f525eabaf0
///
/// @example Simple card div with the link directly inside.
/// <div class="fullclick">
/**
* @file
* Tooltip plugin.
*/
(function (Drupal, CKEDITOR) {
CKEDITOR.plugins.add('tooltip', {
icons: 'tooltip',
init: function (editor) {
// Stop now if not allowed to use editor.
@MatthieuScarset
MatthieuScarset / drupal_dialog_buttonpane.md
Created March 10, 2022 14:27 — forked from opi/drupal_dialog_buttonpane.md
Drupal: Do not put buttons into modal/dialog button pane