Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 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 / scratch.txt
Created October 22, 2017 21:42
Drupal 8, update entity scheme
\Drupal::service('entity.definition_update_manager')->applyUpdates();
@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.
@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 / 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 / .php
Last active May 4, 2018 19:59
Drupal 8, attach IJ for works with popups.
$output['#attached']['library'][] = 'core/drupal.dialog.ajax';