Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Greg-Boggs / README.md
Last active August 30, 2023 10:09
Drupal Site Building Best Practices

Better Drupal Building

  • Custom glue should be accomplished with configuration first and PHP code second.

Configuration Management and Dependencies

  • Use Composer (or Drush Make) to build your project and it's depencies.
  • Store your work in files.
  • Set your config directory above the webroot.
  • Sync UUIDs across all developers.

Theming

@Greg-Boggs
Greg-Boggs / purge_cf.php
Last active August 7, 2023 09:23
PHP code to Purge Cloudflare Cache
<?php
// Replace EMAIL/API_KEY/ZONE_ID with your details.
// Zone ID is on the dashboard for the domain in the bottom right.
// Api keys are generated from the account settings. You must give cache purge permissions
// Place this script on your webserver and point a Github Webhook at it, and you'll clear
// the Cloudflare cache every time you do a push to GH.
$zoneId = "xxx";
$apiKey = "xxx";
$email = "xxx";
@Greg-Boggs
Greg-Boggs / module.php
Created May 17, 2023 18:05
run a drupal hook only once
<?php
/**
* Implements hook_form_alter().
*/
function search_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$requestStack = \Drupal::service('request_stack');
// Get the current request object.
$request = $requestStack->getCurrentRequest();
@Greg-Boggs
Greg-Boggs / digital-library-drupal.md
Last active January 27, 2023 06:46
Digital library of the future with Drupal

Building the Digital Library of the Future with Drupal: A Seamless and Accessible Experience for Patrons

Integrating Drupal with the patron database

Accessing Library Services from Home: Increasing Accessibility and Convenience for Patrons

Building a Digital Catalog: Enhancing Efficiency and Speed of the Checkout Process

Personalized Dashboard for Patrons: Keeping Track of Account Information and Upcoming Due Dates

How do I create a route with a page callback in Drupal9?

To create a route file with a page callback in Drupal, you can follow these steps:

Create a new file in your module's src/Routing directory, named module_name.routing.yml. In this file, define your route using the YAML syntax. An example of a basic route might look like this:

module_name.example:
 path: '/example'
@Greg-Boggs
Greg-Boggs / layouts.module.php
Last active November 13, 2022 23:40
layout switcher for Drupal
<?php
function lib_layouts_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'landing_page' && $view_mode == 'full') {
if ($entity->hasField('field_layout')) {
$field = $entity->get('field_layout');
if (!$field->isEmpty()) {
$layout = $entity->get('field_layout')->getString();
$available_modes = \Drupal::service('entity_display.repository')->getViewModes('node');
if (array_key_exists($layout, $available_modes)) {
@Greg-Boggs
Greg-Boggs / test.php
Created June 29, 2022 21:58
get a link field value in drupal
<?php
$value = NULL;
if (
$entity->hasField($field)
&& $entity->get($field)->first()
&& !empty($entity->get($field)->first()->getValue())
) {
$value = array_values($entity->get($field)->first()->getValue())[0];
}
@Greg-Boggs
Greg-Boggs / example.php
Last active June 24, 2022 02:38
Get a list of all fields in Drupal 9/10
<?php
// Get all Node types and all fields
$content_types = NodeType::loadMultiple();
foreach ($content_types as $content_type) {
$types_options[$content_type->id()] = $content_type->label();
foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions('node', $content_type->id()) as $field_name => $field_definition) {
if (!empty($field_definition->getTargetBundle())) {
$field_options[$field_definition->getName() . ':' . $field_name] = $field_definition->getLabel();
//display content type names in the drop down
@Greg-Boggs
Greg-Boggs / composer.json
Last active June 13, 2022 17:14
composer depends trouble with feeds (also happens with menu json api
{
"name": "drupal/recommended-project",
"description": "Project template for Drupal 9 projects with a relocated document root",
"type": "project",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/drupal",
"support": {
"docs": "https://www.drupal.org/docs/user_guide/en/index.html",
"chat": "https://www.drupal.org/node/314178"
},
@Greg-Boggs
Greg-Boggs / copy fields.php
Created September 22, 2021 06:32
drupal 9 custom module to copy field data on save
<?php
/**
* Implements hook_ENTITY_TYPE_presave().
*
* Prefill cost information for events if a previous event matches the same name and presenter.
*/
function libevents_node_presave(EntityInterface $node) {
$title = '';