Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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 = '';
@Greg-Boggs
Greg-Boggs / module.php
Created September 15, 2021 23:07
Save the fields from the previous node to the new node when they have the same title and presenter
<?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 = '';
$presenter = '';
name: PHP Composer
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
tests: