Skip to content

Instantly share code, notes, and snippets.

View fredysan's full-sized avatar

Fredy Sánchez Téllez fredysan

View GitHub Profile
@fredysan
fredysan / Controller.php
Created February 1, 2024 21:59 — forked from ozin7/Controller.php
Drupal 8: Dependency injection in Service, Controller, Plugin, Deriver.
<?php
/**
* Controller. Implements ContainerInjectionInterface
*/
namespace Drupal\gurei\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\gurei\Service\CompanyService;
use Symfony\Component\DependencyInjection\ContainerInterface;
@fredysan
fredysan / mymodule.install
Last active October 26, 2023 20:43
Drupal 9/10 Change type of fields with existing data
<?php
use \Drupal\field\Entity\FieldStorageConfig;
use \Drupal\field\Entity\FieldConfig;
function mymodule_update_9001() {
$database = \Drupal::database();
$table = 'node__field_name';
$entity_type = 'node';
$field_name = 'field_name';
@fredysan
fredysan / AddFieldProperty.php
Last active June 14, 2024 07:18
How to add a property to an existing field in Drupal 8/9
<?php
namespace Drupal\my_module;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Adds a new property to an existing field type.
@fredysan
fredysan / DebugAccessController.php
Created February 15, 2022 00:49
Debug Access Permissions
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Debug access.
*/
class DebugAccessController extends ControllerBase {
@fredysan
fredysan / IsOwnerFilter.php
Last active May 28, 2021 21:16
Views Custom Filter - Filters out users based on a Subquery
<?php
/**
* @file
*/
namespace Drupal\my_module\Plugin\views\filter;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator;
@fredysan
fredysan / ReferencedEntityNamesFieldFormatter.php
Last active May 28, 2021 20:20
Views formatter that concatenates the names of referenced entities
<?php
/**
* @file
*/
namespace Drupal\my_module\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
/**
@fredysan
fredysan / ThemeSuggestions.md
Created April 5, 2021 20:38
Theme Suggestions

Theme template suggestions.

Page template suggestions per node bundle

/**
 * Implements hook_theme_suggestions_page_alter().
 */
function my_module_theme_suggestions_page_alter(array &$suggestions, array $variables) {
 $node_revision = \Drupal::routeMatch()-&gt;getParameter('node_revision');
@fredysan
fredysan / AddressFieldGetStateName.md
Last active March 31, 2021 17:39
How to get the full name of a State from an address field

Drupal 8 - Address field.

How to get the full Name of a State from an address Field

  $subdivisionRepo = \Drupal::service('address.subdivision_repository');
  $address = $node->get('field_address')->first()->toArray();
  $city = $address['locality'];
  // $subdivisionRepo->get() returns an object of type CommerceGuys\Addressing\Subdivision\Subdivision.
  $state = $subdivisionRepo->get($address['administrative_area'], [$address['country_code']])->getName();
@fredysan
fredysan / Charts.md
Last active November 25, 2020 14:53
Generate Charts using Drupal, CSV files and Charts.js
@fredysan
fredysan / GitAssumeUnchangedPattern.md
Last active November 6, 2020 16:02
How to assume unchanged multiple files matching a pattern

Update Git index so that it doesn't show locally modified blocks

git update-index --assume-unchanged $(git status --porcelain | awk '$1 == "M" && $2 ~ "block.block" {print $2}' | tr '\n' ' ')

Explanation:

Update the git index to avoid some files that were modified to appear in the status list.