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 / 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 / README.md
Last active May 25, 2022 20:28
React + GSAP component example

This is scaffolding for a React component that can properly use GSAP. We setup a random class selector to pass to GSAP when the component is ready. This assumes also some TailwindCSS classes to provide the styling.

@twfahey1
twfahey1 / README.md
Created May 23, 2022 19:19
Github Actions - Use a Dockerfile to do some work in a step

The idea is that any Docker image can be used inside the Github Action to perform a unit of work. In this example, we use an Docker image that is intended to compile our Sphinx build files. It mounts the work directory, in this case we want it on the image docs folder, launches the container and runs the make html command inside the container, which compiles the code into HTML, and places it into a folder output. The work directory is mounted to the docker container, so even ater it shuts down, we are left with the resulting files. Subsequent steps can access this directory as expected, and do whatever with it, e.g. deploy it to our server.

@twfahey1
twfahey1 / code.php
Created May 16, 2022 17:50
Drupal + PHP - Markup manipulation - Weave text inside of the initial <p> tag of a text field
<?php
use Drupal\Component\Utility\Html;
// For example, we have a node with a "tracking ID" that we want to weave in as an image element to the first paragraph.
// This snippet will alter that markup so when we pass it through to whatever renderer,
// we've leveraged PHP built in XML editing to "weave" this snippet into the first <p> tag
$description = $some_node->body->value;
$tracking_markup = "<img src='whatever' />";
if (!empty($description)) {
// We might get wonky HTML, so we don't want the xml lib warnings to flood watchdog.
@twfahey1
twfahey1 / readme.md
Created May 16, 2022 17:46
XDebug configuration in VS Code

Turn up the vars!

  • Open up launch.json - My favorite way is via Cmd + Shift + P for command pallette, type "launch.json", and there it is!
  • Set in the xdebugSettings key for the php Xdebug settings - Generally we should be able to port over settings in XDebug (Link), e.g.:
{
  // XDebug Launch Configuration settings
  "launch": {
    "version": "0.2.0",
 "configurations": [