Skip to content

Instantly share code, notes, and snippets.

@prashantdsala
prashantdsala / custom.js
Created September 13, 2024 08:26
Drupal show hide field based on the selected value
/**
* @file
* Preview behaviors.
*/
(function ($, Drupal) {
Drupal.behaviors.conditional_fields = {
attach(context) {
// Hide the help text initially.
@prashantdsala
prashantdsala / custom.module
Last active September 12, 2024 12:51
Alter Paragraphs form and apply conditional form fields - Drupal 10.3.x
<?php
// https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields#s-table-of-contents
/**
* Implements hook_field_widget_single_element_[WIDGET_TYPE]_form_alter().
*/
function custom_field_widget_single_element_paragraphs_form_alter(&$element, &$form_state, $context) {
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$field_definition = $context['items']->getFieldDefinition();
$paragraph_entity_reference_field_name = $field_definition->getName();
@prashantdsala
prashantdsala / SDC.txt
Last active August 2, 2024 10:20
SDC in Drupal
1. Creating a schema within your my-component.component.yml is mandatory for modules, but optional for themes.
2. Note you can force all components within your theme to require schemas by adding the following to your themename.info.yml.
enforce_prop_schemas: true
3. You can load additional styles, scripts, and dependencies using the libraryOverrides key within your component’s YML file if necessary.
4. The final directory structure looks something like this https://www.drupal.org/docs/develop/theming-drupal/using-single-directory-components/creating-a-single-directory-component#s-to-create-your-first-single-directory-component-follow-these-steps
5. Only .twig and .yml files are required others are optional
6. Schema follows https://json-schema.org/understanding-json-schema
7. Only themes can override components (modules cannot override
8. For a theme to override a component, use the "replaces" key within the mytheme.component.yml file. e.g. replaces: 'sdc_theme_test:my-card'
9. In addition, both components mu
@prashantdsala
prashantdsala / build.yml
Created July 25, 2024 14:25
Github actions "build" part steps
# create a folder .github/workflows and file name say build.yml
# create a token from Profile settings, Developer settings, Personal access token, Token(classic)
# create secret keys in repo settings "Secrets and variables" -> "Actions"
name: Build and Deploy SSG
on:
repository_dispatch:
types: [deploy]
env:
@prashantdsala
prashantdsala / deploy.yml
Created July 25, 2024 14:22
Github actions "deploy" part steps
# copy public key into authrized keys file: cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# chmod 600 ~/.ssh/authorized_keys
# create secrets in repo settings actions page.
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download build artifacts
@prashantdsala
prashantdsala / gist:759cfd6ca629451c7a2f30275adff521
Last active July 1, 2024 09:49
Renaming a branch both locally and remotely:
# Rename the branch locally
git branch -m old-branch-name new-branch-name
# Push the renamed branch and set upstream
git push origin -u new-branch-name
# Delete the old branch from the remote repository
git push origin --delete old-branch-name
# Clean up local branches (optional)
@prashantdsala
prashantdsala / drupalTimestamp.php
Created February 27, 2024 10:05
Drupal: Get timestamp from date field and current time
<?php
use Drupal\Core\Datetime\DrupalDateTime;
// Check if the event's end date is not present
// or has been passed.
$current_time = new DrupalDateTime('now', 'UTC');
if ($entity->get('field_event_end_date')->isEmpty() || ($current_time->getTimestamp() >= $entity->field_event_end_date->date->getTimestamp())) {
}
?>
@prashantdsala
prashantdsala / settings.php
Created February 4, 2024 05:38
Drupal - Specify path for private files directory
<?php
/**
* Private file path:
*
* A local file system path where private files will be stored. This directory
* must be absolute, outside of the Drupal installation directory and not
* accessible over the web.
*
* Note: Caches need to be cleared when this value is changed to make the
* private:// stream wrapper available to the system.
@prashantdsala
prashantdsala / bltddev.txt
Created December 22, 2023 04:16
Setup Acquia BLT with ddev and Drupal 10
// Following steps are to setup Acquia BLt with Drupal 10 which is a kind of work around till gets resolved.
// These steps assuming you have all pre-requisuites done
// https://docs.acquia.com/blt/install/
// https://docs.acquia.com/blt/install/adding-to-project/
// Main point here is The Drupal root must be in a top-level "docroot" directory.
1. composer create-project --no-interaction --no-install drupal/recommended-project drupalblt
2. cd drupalblt
3. sudo sed -i '' -e "s|web/|docroot/|g" composer.json (replacing web/ with doctroot/ to make docroot the top-level directory)
4. composer require --dev thegbomb/blt-ddev
@prashantdsala
prashantdsala / composer.json
Created December 18, 2023 16:02
Drupal: Clone unreleased version of a module/theme from git using composer
// Dowload form_mode_manager from git and using composer.
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8",
"exclude": [
"drupal/form_mode_manager"
]
},
{