Skip to content

Instantly share code, notes, and snippets.

@init90
init90 / scratch.txt
Created October 22, 2017 21:42
Drupal 8, update entity scheme
\Drupal::service('entity.definition_update_manager')->applyUpdates();
@init90
init90 / scratch.txt
Created October 18, 2017 05:40
Drupal 8, Create new revision.
$f = \Drupal::entityTypeManager()->getStorage('fee_item')->load(42);
$f->set('default_sum', 0);
$f->setNewRevision(TRUE);
$f->revision_log = t('Display fee item in list2.');
$f->setRevisionCreationTime(REQUEST_TIME);
$f->setRevisionUserId(1);
$f->save();
@init90
init90 / Node.php
Created October 13, 2017 06:07
drupal 8, views, custom filter, add new argument to 'provide default argument' in views contextual filter.
<?php
namespace Drupal\node\Plugin\views\argument_default;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@init90
init90 / scratch.txt
Created October 9, 2017 09:22
Drupal 8, clean cart.
use Drupal\commerce_cart\CartManagerInterface;
$store = \Drupal\commerce_store\Entity\Store::load(1);
$cart =\Drupal::service('commerce_cart.cart_provider')->getCart('default', $store);
$cart->delete();
\Drupal::service('commerce_cart.cart_manager')->emptyCart($cart, TRUE);
@init90
init90 / gist:5509b5ef6ed7406920abb5651052dc49
Created September 14, 2017 19:02
Drupal 8 backup database from devel php
use Drupal\Core\Database\Database;
$connection_info = Database::getConnectionInfo('default');
$db = $connection_info['default']['database'];
$u = $connection_info['default']['username'];
$p = $connection_info['default']['password'];
$current_date = date('Y-m-d_H-i-s');
$command = "mysqldump --user=$u --password=$p $db";
$dump = shell_exec($command);
@init90
init90 / HouseForm.php
Created August 24, 2017 15:33
drupal 8, link validate
$post_codes_base_link = '';
$post_code_description = $this->house->get('field_postcode')->getDataDefinition()->getDescription();
if (UrlHelper::isValid($post_code_description, TRUE)) {
$post_codes_base_url = \Drupal\Core\Url::fromUri($post_code_description);
$post_codes_base_url->setOptions(['attributes' => ['target' => 'blank']]);
$post_codes_base_link = \Drupal\Core\Link::fromTextAndUrl(
t('Lithuania postcodes base'),
$post_codes_base_url
);
}
@init90
init90 / LovebooksHelperTraits.php
Created August 8, 2017 08:48
drupal 8, commerce, get currency symbol
/**
* Get currency symbol.
*
* @param object $product_variation
* @return string
*/
public function getCurrencySymbol($product_variation) {
$currency_code = $product_variation->get('price')->currency_code;
$currency = Currency::load($currency_code);
@init90
init90 / scratch.txt
Created July 19, 2017 14:48
drupal work with update scheme version
drush ev "echo drupal_get_installed_schema_version('bendrijusistema')";
drupal_set_installed_schema_version('bendrijusistema', 8001);
drupal_get_installed_schema_version('bendrijusistema');
@init90
init90 / cba_webinar.module
Created July 3, 2017 07:31
Drupal 7, update entity path.
/**
* Implements hook_entity_info_alter().
*/
function cba_webinar_entity_info_alter(&$entity_info) {
// Update webinar event path.
$entity_info['event']['bundles']['webinar']['crud']['add']['path'] = 'event/webinar/add';
$entity_info['event']['bundles']['webinar']['crud']['edit']['path'] = 'event/webinar/%eckentity/edit';
$entity_info['event']['bundles']['webinar']['crud']['delete']['path'] = 'event/webinar/%eckentity/delete';
}
@init90
init90 / global.js
Created March 22, 2017 15:23
Run ctools popup after page load.
Drupal.behaviors.security_questions_popup = {
attach: function(context, settings) {
var need_add_questions = Drupal.settings.global.need_add_security_questions;
if (need_add_questions) {
$(window).bind('load', function() {
$('.security_questions_hide_link').click();
});
}
}