Skip to content

Instantly share code, notes, and snippets.

@init90
init90 / gist:7bee7d16d26c59954a994628827ff07d
Created September 24, 2018 09:58
Drupal 8, attach field collection to some entity. In this example to commerce product entity.
use Drupal\commerce_product\Entity\Product;
use Drupal\field_collection\Entity\FieldCollectionItem;
$product = Product::load(34);
$fc_item = FieldCollectionItem::create([
'field_name' => 'ENTITY_FIELD_COLLECTION_FIELD',
]);
$fc_item->set('field_horizontal_position', $horizontal_pos);
$fc_item->set('field_vertical_position', $vertical_pos);
@init90
init90 / NumberRange.php
Last active August 21, 2018 20:20
Drupal 8, create custom form API element. In the example below, the element outputs two fields for entering a range of numeric values.
<?php
namespace Drupal\builder_module\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
/**
* Provides a form element for numeric range.
*
@init90
init90 / gist:86e28f5e1fac8b7c7227b30f0ac8f89c
Last active August 8, 2018 14:10
Generate Let’s Encrypt Certificates for few domains using certbot
sudo certbot --nginx -d site.com.ua -d www.site.com.ua
more info:
https://www.digitalocean.com/community/tutorials/how-to-set-up-let-s-encrypt-certificates-for-multiple-apache-virtual-hosts-on-ubuntu-14-04
@init90
init90 / global_price_table.module
Created July 24, 2018 08:55
Drupal 7, set patch as admin.
/**
* Implements hook_admin_paths().
*/
function global_price_table_admin_paths() {
$paths = array(
'node/*/global_price_table' => TRUE,
);
return $paths;
}
@init90
init90 / gist:d5fd1e170c3af8290020412f66e2a280
Last active July 12, 2018 12:30
Drupal 8, Add process function to managed_file field.
$default_process_fn = \Drupal::service('element_info')->getInfoProperty('managed_file', '#process', NULL);
$form['field_photo'] = [
'#type' => 'managed_file',
'#required' => TRUE,
'#title' => $this->getFieldTitle($product, 'field_photo'),
'#description' => $this->getFieldDescription($product, 'field_photo'),
'#upload_location' => 'public://',
'#process' => array_merge($default_process_fn, [[get_class($this), 'managedFileProcess']]),
'#upload_validators' => [
'file_validate_extensions' => [
@init90
init90 / lb_registration.module
Last active July 13, 2018 21:46
Drupal 7, Disable mail sending if condition is right.
/**
* Implements hook_mail_alter().
*/
function MYMODULE_mail_alter(&$message) {
if ($message['key'] == 'register_confirmation_with_pass') {
$user = $message['params']['account'];
// If user account need to approve we ignore standard mail after registration
// because we have custom mails for this case.
if (lb_registration_account_need_to_approve($user)) {
@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']);
@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 / 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 / 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);