Skip to content

Instantly share code, notes, and snippets.

View rang501's full-sized avatar

Märt Rang rang501

  • Estonia
View GitHub Profile
@rang501
rang501 / gist:f7622418bb07e47b4b04
Created February 5, 2015 18:53
Rendering panelizer programmatically
module_load_include('inc', 'page_manager', 'plugins/tasks/node_view');
// Set page manager to standard, prevents ipe conflicts.
$entity[$entity_key[0]]->panelizer['page_manager']->pipeline = "standard";
$pm = &$entity[$entity_key[0]]->panelizer['page_manager'];
// Page tabs have wrong links, better to disable them.
foreach ($pm->display->content as &$content) {
if ($content->type == 'page_tabs') {
$content->shown = FALSE;
}
@rang501
rang501 / gist:058dd69019435c3da9c1
Created February 13, 2015 09:09
Add css class to detect panels page.
function THEME_preprocess_html(&$variables) {
$panels = page_manager_get_current_page();
$variables['classes_array'][] = (!$panels ? 'not-' : '') . 'panels-page';
}
@rang501
rang501 / gist:223d8b6f7de0e2fa07ba
Created March 31, 2015 13:08
remove menus, languages from drupal programmatically
if (conf_path() != 'sites/default') {
$menus = array(
'menu-main-menu-bg',
'menu-main-menu-cs',
'menu-main-menu-de',
'menu-main-menu-fi',
'menu-main-menu-hr',
'menu-main-menu-hu',
'menu-main-menu-pl',
'menu-main-menu-ro',
@rang501
rang501 / template.php
Created May 7, 2015 10:49
better main menu with bootstrap theme.
function my_theme_preprocess_page(&$variables) {
if ($variables['main_menu']) {
// Build links.
$links_source = variable_get('menu_main_links_source', 'main-menu');
$tree = menu_tree_all_data($links_source, NULL, 1);
menu_tree_add_active_path($tree);
$variables['primary_nav'] = menu_tree_output($tree);
// Provide default theme wrapper function.
$variables['primary_nav']['#theme_wrappers'] = array('menu_tree__primary');
@rang501
rang501 / gist:6db8b6f59b40a8a535d5
Last active December 4, 2015 14:46
Update node field programmatically
<?php
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article');
$result = $query->execute();
if (isset($result['node'])) {
$nodes = entity_load('node', array_keys($result['node']));
@rang501
rang501 / my_module.php
Last active February 2, 2016 07:44
Fix form errors order.
<?php
/**
* Change error messages order.
*
* We need to change the error messages order, because it's wrong. To do that,
* the session array must be modified. It's not nice, but gets the job done.
*/
function my_module_validate_messages_order($form, $form_state) {
$errors = form_get_errors();
// Skip, if there isn't any form errors.
@rang501
rang501 / 0_reuse_code.js
Created February 6, 2016 08:18
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@rang501
rang501 / module.php
Created February 9, 2016 07:40
Add ckeditor to term localize forms
/**
* Implements hook_form_FORM_ID_alter().
*
* Add wysiwyg editor to term translation.
*/
function module_form_i18n_string_translate_page_form_alter(&$form, $form_state) {
$keys = array_keys($form['strings']['all']);
$first = reset($keys);
if (strpos($first, 'taxonomy:term:') !== FALSE) {
@rang501
rang501 / date.php
Last active October 18, 2016 11:54
Get correct time from datetime field (D8)
<?php
$date = DrupalDateTime::createFromFormat(DATETIME_DATETIME_STORAGE_FORMAT, 'datetime string', DATETIME_STORAGE_TIMEZONE);
$date->setTimeZone(new DateTimeZone(drupal_get_user_timezone()));
$date->format('d.m.Y H:i');
@rang501
rang501 / file.php
Last active November 18, 2023 00:36
Drupal 8 get image dimensions by applying image style effects. This doesn't cause performance issue because the dimensions are calculated without creating the actual image.
$image = \Drupal::service('image.factory')->get($file->getFileUri());
$image_style = \Drupal\image\Entity\ImageStyle::load('gallery_large');
// Set source image dimensions.
$dimensions = [
'width' => $image->getWidth(),
'height' => $image->getHeight(),
];
// After calling this, the $dimensions array will contain new dimensions.
$image_style->transformDimensions($dimensions, $file->getFileUri());