Skip to content

Instantly share code, notes, and snippets.

View twfahey1's full-sized avatar

Tyler Fahey twfahey1

View GitHub Profile
@twfahey1
twfahey1 / README.md
Last active December 10, 2024 13:46
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 / uuid.sh
Created January 22, 2022 14:06
Pure bash UUID generator
# Generate a pseudo UUID
uuid()
{
local N B T
for (( N=0; N < 16; ++N ))
do
B=$(( $RANDOM%255 ))
if (( N == 6 ))
@twfahey1
twfahey1 / get.php
Created March 8, 2022 21:54
Drupal - Get URL from node
<?php
use Drupal\Core\Url;
$url_options = [
'absolute' => TRUE,
'language' => \Drupal::languageManager()->getCurrentLanguage(),
];
$link = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $url_options)->toString();
@twfahey1
twfahey1 / dates.php
Created April 19, 2022 01:10
Common best PHP dates formats
<?php
/**
These are just reference for common PHP dates.
**/
// 1/1/2022 12:00 PM
$date = date('m/d/Y h:i A');
@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": [
@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 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 / 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 / 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 / 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;
/**