Skip to content

Instantly share code, notes, and snippets.

@init90
init90 / importForm.php
Created January 2, 2018 12:52
Drupal 8, create link to current page.
$form['preview']['cancel'] = [
'#type' => 'link',
'#title' => $this->t('Cancel'),
'#url' => \Drupal\Core\Url::fromRoute('<current>'),
'#options' => [
'attributes' => [
'class' => [
'btn',
'btn-primary',
'btn-one-row',
@init90
init90 / CommunitySwitcherForm.php
Last active May 4, 2018 19:47
Drupal 8, reset cache in \Drupal::state()
$this->state->resetCache();
$this->state->set("current_community_{$this->current_user->id()}", $community_id);
@init90
init90 / gist:30fb4ef97c4aef0e251c1b1f0406f9af
Last active May 4, 2018 19:43
Drupal 7, field view alter.
/**
* Implements hook_field_attach_view_alter().
*/
function MODULE_field_attach_view_alter(&$output, $context) {
foreach (element_children($output) as $field_name) {
if ($field_name == 'field_cmrc_price_table') {
// If min and max price have the same value, then display price without range.
if (isset($output['field_cmrc_price_table'][0]['#markup']) && !empty($output['field_cmrc_price_table'][0]['#markup'])) {
$separated_price = explode('–', $output['field_cmrc_price_table'][0]['#markup']);
@init90
init90 / commerce_customization.module
Created February 13, 2018 14:30
Drupal 8, check if image is valid.
$image_factory = \Drupal::service('image.factory');
$image = $image_factory->get($file->getFileUri());
if (!$image->isValid()) {
}
@init90
init90 / template.php
Last active May 4, 2018 19:35
Drupal 7. Preproccess colorbox images.
/**
* Implements template_preprocess_field
*/
function THEME_preprocess_field(&$vars) {
// Display first image in colorbox gallery preview.
if (isset($vars['element']['#field_name']) && $vars['element']['#field_name'] == 'field_product_image') {
if (isset($vars['items'][0]['#theme']) && ($vars['items'][0]['#theme'] == 'colorbox_image_formatter')) {
$first_photo_copy = $vars['items'][0];
// Disable copy image in gallery, use only for preview.
@init90
init90 / gist:18f01ac262738aaf184d4dfae34d84a3
Last active May 4, 2018 19:33
Drupal 7, Updated breadcrumb for views page.
/**
* Implements hook_views_pre_render().
*/
function MODULE_views_pre_render(&$view) {
if ($view->name == 'brand_page') {
$breadcrumbs = array();
$breadcrumbs[] = l(t('Home'), '<front>');
drupal_set_breadcrumb($breadcrumbs);
}
}
@init90
init90 / gist:1c9c36607928e350a665aaa740e4e8ba
Last active May 4, 2018 19:49
Drupal 7, Prepared options in views explose filter. Display options only with content. Module for the same - Views Selective Filters.
/**
* Implements hook_form_alter().
*/
function MODULE_form_alter(&$form, &$form_state, $form_id) {
// We prepared product type filter, code below must display only node types
// which have added content.
if (isset($form['#id']) && $form['#id'] == 'EXPLOSE_FILTER_FORM_ID') {
$selected_brand = menu_get_object('taxonomy_term', 2);
@init90
init90 / gist:2bd9a2092ce5863d54d811366ed95896
Last active May 4, 2018 19:19
Drupal, if views filter without options we remove it from output on hook_form_alter().
if (isset($form['#id']) && $form['#id'] == 'VIEW_FILTER_ID') {
$filters_list = array();
// Get filters names.
foreach($form['#info'] as $filter_name => $filter_info) {
// Return filter name without 'filter-' prefix.
$filters_list[] = substr($filter_name, 7);
}
foreach($filters_list as $filter) {
@init90
init90 / scratch.txt
Last active July 13, 2018 21:53
Drupal 7, get all commerce products which have certain taxonomy term. In this case term with id: 52.
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'commerce_product')
->fieldCondition('field_brand', 'tid', 52)
->execute();
@init90
init90 / lb_registration.module
Last active July 13, 2018 21:48
Drupal 8, clean menu cache. In this case for 'account' menu.
// Clean 'account' menu cache.
\Drupal\Core\Cache\Cache::invalidateTags(['config:system.menu.account']);