This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dependencies: | |
// - pathauto:pathauto | |
<?php | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Entity\EntityInterface; | |
use Drupal\pathauto\PathautoState; | |
use Drupal\Core\Render\Element; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
NewerOlder