Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / Drupal 8 - Validate a minimum number of paragraphs
Last active March 25, 2025 06:27
Drupal 8 - Validate a minimum number of paragraphs
/**
* implements hook_form_node_form_alter()
* @author Daniel Laufer
* Add validation to the Resource Center content types.
*/
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$form['#validate'][] = '_mymodule_node_form_validate';
}
@DanLaufer
DanLaufer / Drupal 8 - Programmatically update canonical urls
Last active October 19, 2018 11:53
Drupal 8 - Programmatically find custom canonical urls and set default
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists('field_meta_tags')
->execute();
$count = 0;
foreach($node_ids as $node_id) {
@DanLaufer
DanLaufer / Responsive YouTube Videos
Last active October 19, 2018 11:54
Responsive YouTube Videos
<!-- https://www.ostraining.com/blog/coding/responsive-videos/ -->
<div class="video-responsive">
<iframe width="420" height="315" src="http://www.youtube.com/embed/6xisazZX9bA" frameborder="0" allowfullscreen></iframe>
</div>
<style>
.video-responsive{
overflow:hidden;
padding-bottom:56.25%;
@DanLaufer
DanLaufer / script.php
Last active April 3, 2022 19:57
Drupal 8 - Programmatically Update Boolean Fields
<?php
// Single
use Drupal\node\Entity\Node;
$node = Node::load(1537);
$node->set('field_include_pop_up_form',0); // 0 to uncheck, 1 to check
$node->save();
drupal_set_message($node->get('field_include_pop_up_form')->value);
@DanLaufer
DanLaufer / Drupal 8 - Add paragraph reference to nodes without it already
Last active October 22, 2021 05:36
Drupal 8 - Add paragraph reference to nodes without one
use Drupal\paragraphs\Entity\Paragraph;
$query = \Drupal::entityQuery('node');
// get all blogs with the flexible content field
$node_ids = $query
->condition('status', 1)
->condition('type','insight')
->exists('field_flexible_content')
->execute();
@DanLaufer
DanLaufer / Drupal 8 - Check Nodes for a Specific Form Reference
Last active October 19, 2018 12:04
Drupal 8 - Check Nodes for a Specific Form Reference
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists('field_form_reference')
->execute();
foreach($node_ids as $node_id) {
$node = \Drupal\node\Entity\Node::load($node_id);
$referenced_form = $node->get('field_form_reference')->referencedEntities();
@DanLaufer
DanLaufer / Export JSON PHP
Last active February 23, 2019 18:17
Drupal 8 - Export content as JSON
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists('field_sidebar')
->execute();
$all_nodes_JSON = [];
foreach($node_ids as $node_id) {
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists('field_sidebar')
->execute();
$count_fixed = 0;
$node_ids_with_related_links = [];
@DanLaufer
DanLaufer / Adjust Brightness with FFMPEG
Last active March 3, 2019 09:29
ffmpeg - Adjust Video Brightness Programmatically
ffmpeg -i movie.mp4 -vf eq=brightness=0.1 -c:a copy movie-brighter.mp4
@DanLaufer
DanLaufer / Drupal 8 - Twig - Get an alternating class within a paragraph entity
Last active October 19, 2018 12:09
Drupal 8 - Twig - Get an Alternating Class from Paragraphs in Twig
{% set alternating_class = (_context.elements['#paragraph']._referringItem.getName()) % 2 ? ' teaser__container--inverse' : '' %}