Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / .php
Last active May 4, 2018 19:59
Drupal 8, attach IJ for works with popups.
$output['#attached']['library'][] = 'core/drupal.dialog.ajax';
@init90
init90 / gist:a785f1a0f5972ad1b4a01d45a69c2f3d
Last active May 4, 2018 20:09
Drupal 8, entity uninstall.
$entity_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $entity_update_manager->getEntityType('bs_mail_logger');
$entity_update_manager->uninstallEntityType($entity_type);
@init90
init90 / gist:8324d773d502ca5801608e20a1327e86
Last active May 4, 2018 20:10
PHP get month interval.
public static function getMonthInterval($date) {
$month_interval = [];
// Get start month date.
$date_obj = new \DateTime($date);
$date_obj->modify('first day of this month');
$month_interval['start'] = $date_obj->getTimestamp();
// Get end month date.
$date_obj->modify('last day of this month');
@init90
init90 / group_revision.install
Created October 23, 2017 14:02
Drupal 8, allow run hook update after module install and example how run batch update in drupal 8.
<?php
use Drupal\Core\Database\Database;
use Drupal\group_revision\GroupRevisionTrait;
/**
* Implements hook_install().
*/
function group_revision_install() {
// Update entity fields.