Skip to content

Instantly share code, notes, and snippets.

@jmolivas
jmolivas / inject-service-drupal-8.md
Last active December 26, 2023 20:21
Inject a service from the service container

Inject a Service in Drupal 8

Is a good practice to inject a service whenever is possible.

You can verify the service name by:

Looking at the Drupal Static Service Container wrapper class.

Reading the code on the Drupal Class you can find the httpClient method:

 /**
@jmolivas
jmolivas / DefaultController.php
Last active August 20, 2022 18:57
Drupal 8 example: How to render a Twig template and load a CSS file from a Controller
<?php
namespace Drupal\acme\Controller;
use Drupal\Core\Controller\ControllerBase;
class DefaultController extends ControllerBase
{
/**
@jmolivas
jmolivas / script.js
Last active February 6, 2021 16:21
Trigger Netlify build from a Google Spreadsheet
# From your Google Spreadsheet, select the menu item Tools > Script editor.
# Copy and paste this code.
# Replace uuid with the build_hooks uuid from your Netlify project.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Scripts')
.addItem('Build', 'build')
.addToUi();
}
@jmolivas
jmolivas / crawl-drupal-jsonapi.sh
Created June 12, 2020 19:20 — forked from KalleVuorjoki/crawl-drupal-jsonapi.sh
Script to crawl Drupal JSON:API endpoint to local filesystem.
#!/bin/bash
##
# Script to crawl Drupal JSON:API endpoint to local filesystem.
#
# Assumes endpoint to be /jsonapi and uses basic auth.
#
# Requirements:
# ag - https://github.com/ggreer/the_silver_searcher
# jq - https://github.com/stedolan/jq
exports.createResolvers = ({ actions, getCache, createNodeId, createResolvers }) => {
const { createNode } = actions
createResolvers({
Drupal_MediaImage: {
gatsbyImageFile: {
type: `File`,
resolve(source) {
return createRemoteFileNode({
@jmolivas
jmolivas / install-fish.sh
Last active May 1, 2020 13:06
Improve your shell with fish and oh my fish.
brew install fish
<?php
/**
* @file
* Contains \Drupal\my_module\Plugin\rest\resource\MyRestResource.
*/
namespace Drupal\my_module\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
@jmolivas
jmolivas / gatsby-node.js
Last active February 7, 2020 22:52
Drupal + GraphQL + Gatsby code snippets
const path = require("path");
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
// GraphQL Schema customization to download and attach images to GraphQL node
exports.createResolvers = (
{
actions,
cache,
createNodeId,
createResolvers,
@jmolivas
jmolivas / AccountController.php
Last active November 28, 2019 23:07
Custom JSON Controller
<?php
namespace Drupal\jsonapi_custom\Controller;
use Drupal\Core\Controller\ControllerBase;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\jsonapi_extras\EntityToJsonApi;