Skip to content

Instantly share code, notes, and snippets.

View oksana-c's full-sized avatar

Oksana Cyrwus oksana-c

View GitHub Profile
@oksana-c
oksana-c / gist:087a59fb3853cdab30d3dc6033edcc37
Created August 9, 2018 22:07 — forked from grasmash/gist:9746671
Behat FeatureContext.php for Drupal
<?php
use Drupal\DrupalExtension\Context\DrupalContext,
Drupal\DrupalExtension\Event\EntityEvent,
Drupal\Component\Utility\Random;
use Behat\Behat\Context\BehatContext,
Behat\Behat\Context\Step,
Behat\Behat\Context\Step\Given,
Behat\Gherkin\Node\PyStringNode,
@oksana-c
oksana-c / any.php
Created August 20, 2018 17:24 — forked from robdecker/any.php
Drupal 7: Link l() function with attributes.
l(
t('<span></span>Link Title'),
'link_path',
array(
'attributes' => array(
'class' => array('menu-link'),
'id' =>'faq-page',
),
'html' => TRUE,
),
@oksana-c
oksana-c / MODULENAME.install.php
Created August 29, 2018 21:56 — forked from daggerhart/MODULENAME.install.php
Drupal 8 file field to media entities simple migration. Does not move files in filesystem, just creates new media entities.
<?php
/**
* Create media entities from existing file fields.
*
* @link https://chromatichq.com/blog/migrating-drupal-file-fields-media-entities-without-migrate-module
*/
function MODULENAME_update_8001() {
// Nodes types that will get media migrated.
$node_types = ['article','event','page','session','sponsor'];
// Map old file fields => new media fields.
@oksana-c
oksana-c / gen-d8-salt.sh
Created September 20, 2018 17:59 — forked from pfaocle/gen-d8-salt.sh
Generate Drupal 8 hash salt
drush eval "var_dump(Drupal\Component\Utility\Crypt::randomBytesBase64(55))"
@oksana-c
oksana-c / ExampleAttachTermsProgrammatically.php
Created October 4, 2018 17:15 — forked from dreambubbler/ExampleAttachTermsProgrammatically.php
Drupal 8: Attach terms to entity programmatically
<?php
use Drupal\node\Entity\Node;
/**
* Before attaching a term(s) to a term reference field,
* Must know:
* - field_example_name: the full name of the term reference field
* - tid: the term ID(s) to attach
*
* Keep in mind that this example uses Node::load()
@oksana-c
oksana-c / hook_inline_entity_form_table_fields_alter.php
Last active October 9, 2018 19:14
Display a field from a custom entity in the Inline Entity Form widget table.
<?php
/**
* Implements hook_inline_entity_form_table_fields_alter().
*
* Displays a field from a custom entity in the Inline Entity Form windget table.
*/
function MODULENAME_inline_entity_form_table_fields_alter(&$fields, $context) {
// Display Entity field in a column within Inline Entity Form widget.
if ($context['parent_bundle'] == 'content_type_parent_to_ief' && $context['field_name'] == 'entity_reference_field_name') {
// Unset original IEF label if needed.
@oksana-c
oksana-c / MyService.php
Created October 29, 2018 22:47 — forked from JeffTomlinson/MyService.php
Drupal 8 Configuration Dependency Injection
<?php
namespace Drupal\my_module\Services;
use Drupal\Core\Config\ConfigFactory;
/**
* Class MyService.
*
* @package Drupal\my_module\Services
@oksana-c
oksana-c / d8-preprocess-process-hooks.md
Last active November 8, 2018 22:07
D8: Defining preprocess & process hooks

contents of the theme/preprocess/README.md

Defining preprocess hooks

Rather than placing your preprocess hooks directly in the .theme file you can manage them in automatically discovered and lazy-loaded include files. It is even possible to organize them in sub-folders. This feature greatly improves the maintainability of large themes that would otherwise contain hundreds of lines of unrelated code in your template.php file.

@oksana-c
oksana-c / alter_solarium_query.php
Last active November 13, 2024 11:09
Search API Solr - hook_search_api_solr_converted_query_alter()
<?php
/**
* Implements hook_search_api_solr_converted_query_alter().
*
* Modifies Solarium query to group results by field.
* Modifies Solarium query to sort results by first geofield value within the multivalue field.
*/
function MODULE_search_api_solr_converted_query_alter(\Solarium\Core\Query\QueryInterface $solarium_query, \Drupal\search_api\Query\QueryInterface $query) {
// Alter the Solarium query.
@oksana-c
oksana-c / import.php
Created November 17, 2018 18:10 — forked from crittermike/import.php
Importing Drupal 8 config programmatically
<?php
// Import arbitrary config from a variable.
$config = \Drupal::service('config.factory')->getEditable('filter.format.basic_html');
$config->setData($data)->save();
// Or, re-import the default config for a module or profile, etc.
\Drupal::service('config.installer')->installDefaultConfig('module', 'my_custom_module');
// Or, import YAML config an arbitrary directory.