Skip to content

Instantly share code, notes, and snippets.

View slivorezka's full-sized avatar

Andriy Malyeyev slivorezka

View GitHub Profile
@slivorezka
slivorezka / get_image_path_with_image_style.php
Created November 8, 2016 10:40
Drupal 8: Get Image URL / URI with Image Style
<?php
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
// File ID.
$fid = 123;
// Load file.
$file = File::load($fid);
// Get origin image URI.
$image_uri = $file->getFileUri();
// Load image style "thumbnail".
@slivorezka
slivorezka / allow_alter_your_date.php
Created November 28, 2016 11:08
Drupal 8: Allow other modules to alter your data
<?php
// Allow other modules to alter your data.
$data = [
'#type' => 'markup',
'#markup' => t('This is the default text.'),
];
\Drupal::moduleHandler()->alter('MODULE_some_action', $data);
/**
* Implements hook_MODULE_some_action_alter().
@slivorezka
slivorezka / get_entity_by_alias.php
Created March 29, 2017 18:26
Drupal 8: Get Entity By Alias
<?php
// Some entity alias (node, taxonomy_term, user, etc).
$alias = '/about';
// Get URL obj.
$url = Url::fromUri('internal:' . $alias);
// Check exist alias.
if ($url->isRouted()) {
$params = $url->getRouteParameters();
$entity_type = key($params);
// GEt entity.
@slivorezka
slivorezka / get_term_by_name.php
Created November 8, 2016 16:24
Drupal 8: Load term by name
<?php
// Load term by name
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => 'some_name']);
@slivorezka
slivorezka / hook_preprocess_views_view_field.php
Created March 22, 2018 13:37
hook_preprocess_views_view_field
<?php
/**
* Implements hook_preprocess_views_view_field().
*/
function MODULE_preprocess_views_view_field(&$variables) {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
if ($view->id() == 'schedule') {
if ($view->current_display == 'embed_1') {
@slivorezka
slivorezka / memcache.php
Last active December 7, 2018 14:16
Memcache
if (class_exists('Memcached', FALSE)) {
$class_loader->addPsr4('Drupal\\memcache\\', 'modules/contrib/memcache/src');
$settings['bootstrap_container_definition'] = [
'parameters' => [],
'services' => [
'settings' => [
'class' => 'Drupal\Core\Site\Settings',
'factory' => 'Drupal\Core\Site\Settings::getInstance',
],
$values = FieldConfig::loadByName('node', 'BUNDLE', 'FIELD')->getSetting('allowed_values');
@slivorezka
slivorezka / TestAjaxFormSelect.php
Created September 21, 2018 07:32 — forked from jez500/TestAjaxFormSelect.php
Drupal 8 Ajax form example when select box changed
<?php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class TestAjaxFormSelect.
*/
@slivorezka
slivorezka / AddAnotherItem.php
Created September 21, 2018 07:32 — forked from leymannx/AddAnotherItem.php
Drupal 8 Ajax Form Add Item Example
<?php
/**
* @file
* Contains \Drupal\hello_world\Form\AddAnotherItem.
*/
namespace Drupal\hello_world\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
@slivorezka
slivorezka / drupal_8_most_useful_service.php
Last active September 11, 2018 05:12
Drupal 8: Most useful services
<?php
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The menu link tree service.
*
* @var \Drupal\Core\Menu\MenuLinkTree