Skip to content

Instantly share code, notes, and snippets.

@BERRAMOU
BERRAMOU / HeaderView.php
Last active March 8, 2019 15:31
Drupal 8 Twig extension to get render the header of View.
<?php
namespace Drupal\MYMODULE\Twig;
use Drupal\views\Views;
/**
* Adds extension to render a view.
*
* @package Drupal\twig_views\Twig.
@BERRAMOU
BERRAMOU / CustomSerializer.php
Created March 8, 2019 11:16
Drupal 8 : Custom serializer example to render result as array instead of objects.
<?php
namespace Drupal\MY_MODULE\Plugin\views\style;
use Drupal\rest\Plugin\views\style\Serializer;
/**
* The style plugin for serialized output formats.
*
* @ingroup views_style_plugins
*
@BERRAMOU
BERRAMOU / example.php
Created March 14, 2019 22:47 — forked from rocketeerbkw/example.php
How to migrate multi-value link field in Drupal 8
<?php
class Example extends SourcePluginBase {
public function prepareRow(Row $row) {
parent::prepareRow($row);
// I do some data manipulation to end up with an array that looks like this,
// which I want to import into multi-value link field.
$links = [
@BERRAMOU
BERRAMOU / SearchResults.php
Created March 28, 2019 14:52 — forked from kevinquillen/SearchResults.php
Example of a Controller using SearchAPI to generate a results page (without Views).
<?php
namespace Drupal\velirsearch\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\velirsearch\Service\PagerService;
class SearchResults extends ControllerBase {
@BERRAMOU
BERRAMOU / custom_encrypt_decrypt.php
Created April 18, 2019 10:49
Drupal 7 encrypt decrypt string example
<?
/**
* Function to encrypt and decrypt a string.
* @param $string
* @param string $action
*
* @return bool|string
*/
function _custom_encrypt_decrypt($string, $action = 'encrypt') {
$secret_key = 'k;Yj>+kZ@CHBJQDs*G=2!uRXE+4;)H(M';
@BERRAMOU
BERRAMOU / filterByYearViews.php
Last active July 8, 2019 14:05
Filter by year in date even if it has other formats then YYYY, just add the field as exposed filter with regex Operator, and then adapt the bellow code to your case.
<?php
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_form_FORM_ID_alter().
*/
function myModule_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
@BERRAMOU
BERRAMOU / getNAvToken.php
Last active September 21, 2019 21:28
Get navToken for navionics webapi-v2 from api key.
<?php
function get_navtoken() {
// My api key.
$api_key = 'Navionics_webapi_someNumber';
$url = 'https://backend.navionics.io/tile/get_key/'.$api_key.'/webapiv2.navionics.com';
// Authorized domain by the navionics.
$referer = 'https://myhost.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@BERRAMOU
BERRAMOU / fix-drupal8-permissions.sh
Created September 13, 2019 09:22 — forked from GreenSkunk/fix-drupal8-permissions.sh
Drupal - Bash shell script to fix Drupal 8 permissions where the PHP Handler is FastCGI.
#!/bin/bash
path=${1%/}
user=${2}
group=${2}
help="\nHelp: This script is used to fix permissions of a Drupal 8 installation in which the PHP Handler is FastCGI\nYou need to provide the following arguments:\n\t 1) Path to your drupal installation\n\t 2) Username of the user that you want to give files/directories ownership\n\nNote: This script assumes that the group is the same as the username if your PHP is compiled to run as FastCGI. If this is different you need to modify it manually by editing this script or checking out forks on GitHub.\nUsage: bash ${0##*/} drupal_path user_name\n"
echo "Refer to https://www.Drupal.org/node/244924"
<?php
/**
* Implements hook_menu().
*/
function MODULENAME_menu() {
$items = [];
$items['api/v1/sample/request'] = [
'page callback' => 'sample_request',
'access callback' => 'user_is_api_user',
@BERRAMOU
BERRAMOU / node.twig.html
Created November 24, 2020 18:48 — forked from leahtard/node.twig.html
Get file url in node for Drupal 8 twig template
{# Get URL of single file field #}
{{ node.field_file.entity.uri.value }}
{# Get URL of multi file field #}
{{ node.field_file['#items'].entity.uri.value }}
{# Trun into link #}
{{ file_url(node.field_file.entity.uri.value) }}
{# Check if there is at least one value #}