Skip to content

Instantly share code, notes, and snippets.

View twfahey1's full-sized avatar

Tyler Fahey twfahey1

View GitHub Profile
@twfahey1
twfahey1 / my_module.install
Created January 11, 2024 14:38
Install a new custom entity in Drupal 10 existing module
<?php
/**
* Scenario: We just added a new content entity via drush generate entity:content. We want to install it.
* The module was already enabled, so we can easily install it with 2 lines.
*/
function superentity_demo_update_8001()
{
$entity_type_manager = \Drupal::entityTypeManager();
\Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type_manager->getDefinition('id_of_my_entity'));
@twfahey1
twfahey1 / README.md
Last active November 25, 2023 23:36
Using Google API credentials.json file in Github Action or other scripts

The question: What is the best way we can use Google API via a service account in Github Actions? Answer: encrypt the credentials and decrypt during Action w/ a configured secret.

  • The credentials.json.gpg is originated from the credentials.json that can be downloaded from Cloud Console for the service account.
  • Encrypt it via: gpg --symmetric --cipher-algo AES256 credentials.json - Note the password used, as it will be added as a secret in this repo to be used for decoding the file and accessing Google APIs.
  • Update the credentials.json.gpg file in this repo using the contents of the newly created credentials.json.gpg, commit and push.
  • The password used should be added as a secret, e.g. the GOOGLE_API_PW secret in the github repo

Then, in the Github action or script, call gpg to decrypt and write the unencrypted file:

#!/bin/sh
@twfahey1
twfahey1 / gh action snippet
Created July 28, 2020 12:39
GitHub actions snippet to install apache
- name: Install Apache
run: |
pwd
sudo apt update
sudo apt install apache2
echo "GETTING ADMIN PERMS SETUP"
sudo adduser $(whoami) www-data
sudo adduser $(whoami) root
@twfahey1
twfahey1 / settings.local.php
Last active October 22, 2023 11:02
Drupal 10 - Symfony Mailer + Mailhog on Docksal - Local settings snippet
<?php
# If you have a SMTP setup, such as for example 'Postmark', where we have a config file `symfony_mailer.mailer_transport.postmark.yml`,
# this override would locally in Docksal make the Mailhog handler for what in prod is Postmark:
$config['symfony_mailer.mailer_transport.postmark']['configuration']['user'] = '';
$config['symfony_mailer.mailer_transport.postmark']['configuration']['pass'] = '';
$config['symfony_mailer.mailer_transport.postmark']['configuration']['host'] = 'mail';
$config['symfony_mailer.mailer_transport.postmark']['configuration']['port'] = '1025';
@twfahey1
twfahey1 / README.md
Last active September 5, 2023 15:00
Drupal 10 - Docksal - Symfony Mailer symfony_mailer - Local dev email config

To send mails to Mailhog on dev-enviroment:

  • Add in settings.local.php:
$config['symfony_mailer.settings']['transport'] = 'smtp://mail:1025';

@twfahey1
twfahey1 / README.md
Created December 14, 2022 14:37
Creating a new content entity in an existing enabled module

Let's say you just generated a content entity with drush:

drush generate entity:content

(... you go through the prompts, and the entity is created in an existing, enabled module)

In the module's .install file, add a hook_update_N(), for example if you previously called your entity a_super_cool_entity

/**
@twfahey1
twfahey1 / ExampleMenuBlock.php
Created November 28, 2022 17:35
Drupal 8, 9+ - Loading a menu programmatically
<?php
namespace Drupal\example_menu\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@twfahey1
twfahey1 / ExampleBlock.php
Created September 13, 2022 02:02
Drupal example - Get a default entity reference target from a field and check if a node has the default value
<?php
// Get the default target_id for this field from the field settings.
$default_target_uuid = $loaded_node->field_some_entity_ref_field->getFieldDefinition()->get('default_value')[0]['target_uuid'];
$default_media_entity = $this->entityRepository->loadEntityByUuid('media', $default_target_uuid);
$default_media_entity_id = $default_media_entity->id();
// Check if is the default one.
$photo = $loaded_node->field_some_entity_ref_field->getValue();
if ($photo[0]['target_id'] == $default_media_entity_id) {
@twfahey1
twfahey1 / example-wp.sh
Last active August 4, 2022 11:04
Pantheon - run php-script (php eval alternative) via terminus against a site
cat hello.php | terminus remote:wp ${SITE}.${ENV} -- eval-file -
Drupal.behaviors.mybehavior = {
attach: function (context, settings) {
$('#some_element', context).once('mybehavior', function () {
// Code here will only be applied to $('#some_element')
// a single time.
});
}
};