Skip to content

Instantly share code, notes, and snippets.

@paboden
paboden / module.php
Last active February 22, 2024 00:18 — forked from gwagroves/module.php
Drupal 8 Paragraphs: Add a theme suggestion based on the paragraph and the type of the parent node / entity.
<?php
/**
* Implements theme_suggestions_HOOK_alter().
*/
function mytheme_theme_suggestions_paragraph_alter(array &$suggestions, array $variables) {
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $variables['elements']['#paragraph'];
/** @var \Drupal\Core\Entity\ContentEntityInterface $parent */
$parent = $paragraph->getParentEntity();
@paboden
paboden / d9_php_grab_first_paragraph
Created November 18, 2022 06:48
Drupal 8/9/php: Grab first paragraph from string of html
/**
* Helper function to grab the first paragraph from body/html string.
*
* This is useful for pulling out the first paragraph from a body or long text field,
* and using it for a short summary or description.
*/
function MYMODULE_grab_summary_paragraph($html_string, $remove_wrapping_p_tags = FALSE) {
// Find start point of first p tag.
$opening_p_point = strpos($html_string, "<p");
// Find end point which will be used as the cutoff length.
@paboden
paboden / d9_force_pathauto_generation
Last active March 28, 2023 02:11
Drupal 8/9: Enforce Pathauto url generation, even if unchecked on node.
//dependencies:
// - pathauto:pathauto
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\pathauto\PathautoState;
use Drupal\Core\Render\Element;
@paboden
paboden / d8_get_and_build_blocks_from_a_region
Created June 15, 2022 02:01
Drupal 8/9: Get the blocks set to a region, or build selected blocks from a reference field and add them to a region.
// EXAMPLE 1
// Get the blocks normally set to display in a particular region of the active theme.
$block_list = [];
$entity_manager = \Drupal::entityTypeManager();
$block_manager = $entity_manager->getStorage('block');
$theme = \Drupal::theme()->getActiveTheme()->getName();
$region = REGION_NAME; // Set the region you want to get the blocks from.
// Get and load the blocks that are set to a region
$values = ['theme' => $theme, 'region' => $region];
@paboden
paboden / d8_create_redirect_response_in_event_subscriber
Created May 27, 2022 20:34
Drupal 8/9: Create a redirect response, using event subscribers, from old url to new url
# This code goes into module/src/EventSubscriber/MyModuleEventSubscriber.php file
<?php
namespace Drupal\MY_MODULE\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
@paboden
paboden / d8_get_file_url_from_media_field
Last active May 26, 2022 18:46
Drupal 8/9: Get the URL of any file inside of a media reference, in any entity
// Use this snippet to get the original URL of images, and the direct file URL of other media types (like documents and videos).
$entity_type_manager = \Drupal::entityTypeManager();
$media_manager = $entity_type_manager->getStorage('media');
$file_manager = $entity_type_manager->getStorage('file');
$generator_service = \Drupal::service('file_url_generator');
$urls = [];
// If the entity has the media reference field in question.
@paboden
paboden / d8_get_field_in_paragraph_in_paragraph_in_node
Created May 13, 2022 19:06
Drupal 8/9: Get a field in a paragraph entity in a paragraph entity inside of a node entity
{% for item in node.field_content %}
<h3>{{item.entity.field_heading.value }}</h3>
<ul>
{% for s in item.entity.field_section_content %}
{% set sub = s.entity.field_sub_heading.value %}
{% set icon = s.entity.field_media_type.value %}
<li class="{{ icon }}"> {{ sub }}</li>
{% endfor %}
</ul>
{% endfor %}
@paboden
paboden / d8_get_vimeo_youtube_ids
Last active April 13, 2022 18:11
Drupal 8/9: Get vimeo or youtube ids for media
/**
* Extracts the vimeo id from a vimeo url.
*
* Returns false if the url is not recognized as a vimeo url.
*/
function get_vimeo_id($url) {
return preg_match('#(?:https?://)?(?:www.)?(?:player.)?vimeo.com/(?:[a-z]*/)*([0-9]{6,11})[?]?.*#', $url, $m) ? $m[1] : false;
}
/**
@paboden
paboden / d8_control_taxonomy_view_display
Created April 13, 2022 17:58
Drupal 8/9: Change the view mode used in taxonomy view
/**
* Implements hook_views_post_build().
*/
function MYMODULE_views_post_build(ViewExecutable $view) {
// Check if we are dealing with the taxonomy term view.
if ($view->id() == 'taxonomy_term' && $view->current_display == 'page_1') {
// Get the term ID from the current page and load up the term entity.
$tid = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
@paboden
paboden / d8_render_facet_block_instance
Created April 13, 2022 17:51
Drupal 8/9: Programmatically render a Facet Block
use Drupal\Core\Plugin\PluginBase;
$facet = 'your_facet_id';
$render = [];
$block_manager = \Drupal::service('plugin.manager.block');
$config = [];
$block_plugin = $block_manager->createInstance('facet_block' . PluginBase::DERIVATIVE_SEPARATOR . $facet, $config);
if ($block_plugin) {
$access_result = $block_plugin->access(\Drupal::currentUser());
if ($access_result) {