Skip to content

Instantly share code, notes, and snippets.

View svetlio's full-sized avatar

Svetoslav Stoyanov svetlio

View GitHub Profile
@svetlio
svetlio / module_name.post_update.php
Last active February 13, 2024 14:20
post_update batch
<?php
/**
* Update existing nodes cp property to false.
*/
function uneo_pcf_cp_post_update_node_cp_prop(&$sandbox) {
$entity_type_id = 'node';
$id_name = 'nid';
_update_entity_cp_property($sandbox, $entity_type_id, $id_name);
}
@svetlio
svetlio / ImportConfigForm.php
Last active February 13, 2024 14:21
Import from module config/optional
<?php
// ...
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$module_path = $this->extensionListModule->getPath('cpct_config');
@svetlio
svetlio / post-receive
Created January 28, 2020 09:21
post-receive script in bare repo hooks to pre-populate code in web-server dir
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [[ "d881" == "$branch" ]]; then
git --work-tree=/var/www/vhosts/fwi.stage/ checkout -f $branch
echo 'Changes pushed d881 branch to fwi.stage'
@svetlio
svetlio / .htaccess
Last active December 19, 2019 21:07
apache2 redirect to https with www (https:/www.)
# source: https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/
# To redirect all users to access the site WITH the 'https://www.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
@svetlio
svetlio / EntityReferenceLabelCustomPathFormatter.php
Last active January 17, 2019 06:32
Field formatter for entity reference label to set link to custom controller or views. Used with terms, !!! not tested for another entities.
<?php
namespace Drupal\tools_ext\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Url;
use Drupal\Component\Serialization\Json;
@svetlio
svetlio / ManageVocabulariesController.php
Created January 16, 2019 13:16
Manage vocabularies page to show vocabularies translation in other languages too.
<?php
namespace Drupal\tools_ext\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
$view = new view();
$view->name = 'files';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'file_managed';
$view->human_name = 'Files';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
@svetlio
svetlio / CustomLoginResource.php
Last active November 9, 2018 09:07
Drupal 8 rest custom login resource, return session data to build cookie in frontend, missing csrf (can be obtained at /rest/session/token). !!! This is a POST resource, so '$ drush cr', enable resource, and add a permission for anonymous role.
<?php
namespace Drupal\exp_fs\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@svetlio
svetlio / FileIdFieldFormatter.php
Created September 8, 2018 07:27
Drupal 8 FieldFormatter for file and image. Output file/image ID. Usable for rest in views. ($ drupal generate:plugin:fieldformatter)
<?php
/**
* @file FileIdFieldFormatter.php
*
* field formatter id for file and image
*
*/
namespace Drupal\custom_fs\Plugin\Field\FieldFormatter;
@svetlio
svetlio / post-receive
Created August 9, 2018 15:33
git push to prepopulate code in webdir
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [[ "master" == "$branch" ]]; then
git --work-tree=/var/www/vhosts/bms2/ checkout -f $branch
echo 'Changes pushed master to bms2'