You are on page 1of 5

Change None text in select list

Get field value


Multiple value
Single value
Dealing with Image entity
Image URL
Entity reference
Get all referenced entities
Drush command workaround on Windows
Running Drush
Drupal console
Running Drupal Console
Composer
Email testing
Tips
Webform
AJAX webform in a modal
Javascript
Add inline javascript
Add drupalSettings
Entity Validation
Reference
ConfigPages
Patch
Applying patch
Reversing patch
Update Drupal
Reference
How to get Views SQL query
Find block content used in Layout Builder

Change None text in select list

if (isset($form['field_hero_bg']['widget']['#options']['_none'])) {
$form['field_hero_bg']['widget']['#options']['_none'] = t('Default background
color');
}

Get field value


Multiple value

$entity->get('field_name')->getValue();

Single value

$entity->get('field_name')->value;

Dealing with Image entity


Image URL

$bgimage_mid = $field_hero_bgimage[0]['target_id'];

/** @var Drupal\media\Entity\Media $media */


$media = Media::load($bgimage_mid);
$media_image = $media->get('image')->getValue()[0];

// Alt text for the image


$bgimage_alt = $media_image['alt'];

// Src for the image


$bgimage_file = File::load($media_image['target_id']);
$bgimage = $bgimage_file->createFileUrl();

Entity reference
Get all referenced entities

$node->get('field_FIELDNAME')->referencedEntities();

Drush command workaround on Windows


Usually required if the site is using Acquia lightning.

After clicking Open Drush Console button on Acquia Dev Desktop, run drush cr command. The console will
display error The filename, directory name, or volume label syntax is incorrect. This is
expected.

After that, you can run command below.

Running Drush

From docroot .

php ..\vendor\drush\drush\drush cr
If it's not working, try running drush cr first before running that command.

Drupal console
Running Drupal Console

From docroot .

php ..\vendor\drupal\console\bin\drupal generate:module

Composer

php -d memory_limit=-1 D:\Apps\DevDesktop\tools\composer.phar require


drupal/swiftmailer

Email testing
Tips

Use reroute email module, route all email to an address. Apply this only in some environments by
using Config Split module
Use Papercut SMTP for local testing

Webform
AJAX webform in a modal

TODO

Javascript
Add inline javascript

$form['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'type' => 'text/javascript',
],
'#value' => 'function timestamp() {
var response = document.getElementById("g-recaptcha-response");
if (response == null || response.value.trim() == "") {
var elems = JSON.parse(document.getElementsByName("captcha_settings")[0].value);
elems["ts"] = JSON.stringify(new Date().getTime());
document.getElementsByName("captcha_settings")[0].value = JSON.stringify(elems);
}
}
setInterval(timestamp, 500);',
],
'ownerform_snippet',
];

Add drupalSettings

$form['#attached']['drupalSettings']['flot_d8'][$div_id] = ['data' => $data, 'options'


=> $options];
}

Entity Validation
Reference

https://makedrupaleasy.com/articles/drupal-8-custom-validator-checking-field-depending-value-anoth
er-field

ConfigPages

use Drupal\config_pages\Entity\ConfigPages;

$myConfigPage = ConfigPages::config('my_config_page');
$variables['my_global_value'] = $myConfigPage
->get('field_something')->value;

Patch
Applying patch

patch -p1 < [patch-name].patch

Reversing patch

patch -R -p1 < [patch-name].patch

Update Drupal
Reference

https://drupalize.me/tutorial/update-drupals-minor-version?p=3233

composer update drupal/core-recommended --with-dependencies


How to get Views SQL query

$query = $view->query->query();

// Format SelectQueryInterface into a string.


$string = (string) $query;

// Replace arguments.
$arguments = $query->arguments();
if (!empty($arguments) && is_array($arguments)) {
foreach ($arguments as $placeholder => &$value) {
if (is_string($value)) {
$value = "'$value'";
}
}
$string = strtr($string, $arguments);
}

// Format the query string for more readable output.


$string = str_replace(array(' {', "\n{"), ' ', $string);
$string = str_replace(array('} ', "}\n"), ' AS ', $string);
$string = str_replace(', ', ",\n ", $string);
$string = str_replace(' AND ', "\n AND ", $string);
$string = str_replace(' ON ', "\n ON ", $string);
$string = str_replace('SELECT ', "SELECT\n ", $string);
$string = str_replace('ORDER BY ', "ORDER BY\n ", $string);

// echo $string;
$out = str_replace(' ', '&nbsp;&nbsp;', nl2br($string));

Find block content used in Layout Builder

# find block uuid


select * from block_content bc where id=2176;

# find node that use this uuid


select nfd.nid, nfd.title
from node__layout_builder__layout nlb
join node_field_data nfd on nlb.`entity_id`=nfd.`nid`
where nlb.layout_builder__layout_section like '%block_content:367267eb-26d8-45d4-bcc4-
8005c82450b9%';

You might also like