Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / edit_this_node.info.yml
Last active June 12, 2019 15:27
Drupal 8 - Edit This Node button
name: Edit This Node
description: Adds a button to the toolbar to edit the current node. It will say "New Draft" if using moderation.
package: Custom
type: module
version: 1.0
core: 8.x
@DanLaufer
DanLaufer / list_text_long_fields.php
Created May 29, 2019 15:55
Drupal 8 - PHP Script to list all fields on all content types of a certain type (ex. text_long)
<?php
// get node types
$node_types = \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple();
$entityManager = \Drupal::service('entity_field.manager');
// for each content type, get the text_long fields for that content type
@DanLaufer
DanLaufer / list_text_long_fields.php
Created May 29, 2019 15:55
Drupal 8 - PHP Script to list all fields on all content types of a certain type (ex. text_long)
<?php
use Drupal\paragraphs\Entity\Paragraph;
// get node types
$node_types = \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple();
$entityManager = \Drupal::service('entity_field.manager');
@DanLaufer
DanLaufer / list_nid_with_paragraph.php
Last active May 14, 2019 15:48
Drupal 8 - List all nids with a specific paragraph in a specific field
<?php
use Drupal\paragraphs\Entity\Paragraph;
$field_containing_paragraph = 'field_my_field';
$paragraph_type_to_find = 'my_paragraph';
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
@DanLaufer
DanLaufer / list_paragraphs_used.php
Last active April 3, 2024 15:10
Drupal 8 - List all paragraphs referenced in a field and frequency used across all content types
<?php
use Drupal\paragraphs\Entity\Paragraph;
$field_name = 'field_my_field';
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists($field_name)
@DanLaufer
DanLaufer / createTaxonomy.php
Last active May 12, 2019 17:13
Drupal 8 - Create Taxonomy terms programmatically from CSV
<?php
use Drupal\taxonomy\Entity\Term;
ini_set('auto_detect_line_endings', TRUE);
$rows = array_map('str_getcsv', file('./scripts/media/taxonomy.csv'));
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row) {
@DanLaufer
DanLaufer / RunScripts.md
Last active May 12, 2019 17:41
Drupal 8 - Run PHP script on a local & remote environments

Run PHP on Lando Environment

Note: you must have <?php at the beginning of the script.

lando drush scr ./myscript

Run PHP on Remote

*Note: you must LEAVE OUT `

@DanLaufer
DanLaufer / queries.sql
Last active May 12, 2019 17:20
Drupal 8 - Some SQL Queries
- List nodes w/o aliases
SELECT nid, type FROM node WHERE nid NOT IN (
SELECT SUBSTR(source, 7) FROM url_alias WHERE source LIKE '/node/%'
);
- List nodes w/o aliases and group by content type
SELECT nid, type FROM node WHERE nid NOT IN (
SELECT SUBSTR(source, 7) FROM url_alias WHERE source LIKE '/node/%'
@DanLaufer
DanLaufer / package.json
Last active May 12, 2019 17:39
Laravel-Mix (webpack) drupal theme setup
{
"name": "my_site",
"version": "1.0.0",
"description": "My Site",
"main": "index.js",
"scripts": {
"dev": "NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"development": "NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"hot": "NODE_ENV=development webpack-dev-server --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
@DanLaufer
DanLaufer / getXMLNodeCount.php
Last active May 12, 2019 17:47
PHP - XML - Get Node Count of a Glob of XML Docs
<?php
$xml_files = glob("./POC_LO_SET/*.xml");
print "starting" . "\n";
$overall_count = array();
//$amount_to_try = 2;
foreach ($xml_files as $file) {