Skip to content

Instantly share code, notes, and snippets.

View levmyshkin's full-sized avatar

Ivan Abramenko levmyshkin

View GitHub Profile
@levmyshkin
levmyshkin / THEMENAME.theme
Last active December 10, 2021 11:39
Get paragraph type in preprocess_paragraph in drupal
<?php
/**
* Implements template_preprocess_paragraph().
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - paragraph: The paragraph object.
* - view_mode: View mode; e.g., 'full', 'teaser'...
@levmyshkin
levmyshkin / BookManager.php
Created November 19, 2021 14:27
Drupal Dependencies injection in a custom class/service
<?php
namespace Drupal\book;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
...
@levmyshkin
levmyshkin / SettingsForm.php
Last active November 19, 2021 14:25
Drupal Dependencies Injection in ConfigFormBase settings form
<?php
namespace Drupal\commerce_order_number\Form;
use Drupal\commerce_order_number\OrderNumber;
use Drupal\commerce_order_number\OrderNumberFormatterInterface;
use Drupal\commerce_order_number\OrderNumberGeneratorManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
@levmyshkin
levmyshkin / ModalForm.php
Created November 19, 2021 14:24
Drupal Dependencies Injection in BaseForm
<?php
namespace Drupal\drupalbook\Form;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Egulias\EmailValidator\EmailValidator;
/**
@levmyshkin
levmyshkin / CartBlock.php
Created November 19, 2021 14:23
Drupal Dependency Injection in Block
<?php
// /modules/custom/drupalbook/src/Plugin/Block/CartBlock.php
namespace Drupal\drupalbook\Plugin\Block;
use Drupal\commerce_cart\CartProviderInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@levmyshkin
levmyshkin / ModalFormContactController.php
Created November 19, 2021 14:22
Drupal Dependency Injection in Controller
<?php
// /modules/custom/drupalbook/src/Controller/ModalFormContactController.php
namespace Drupal\drupalbook\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilder;
@levmyshkin
levmyshkin / DidThisHelp.php
Created November 19, 2021 14:20
Extending custom Drupal Views filter handler
<?php
namespace Drupal\did_this_help\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\InOperator;
/**
* Filters by given list of yes/no options.
*
* @ingroup views_filter_handlers
@levmyshkin
levmyshkin / did_this_help.views.inc
Created November 19, 2021 14:19
Writing integration with Drupal Views
<?php
// from https://www.drupal.org/project/did_this_help
// https://drupalbook.org/drupal/914-writing-integration-views
/**
* @file
* Provide views data for did_this_help.module.
*/
/**
* Implements hook_views_data().
@levmyshkin
levmyshkin / database.php
Created November 19, 2021 14:13
Working with the database in Drupal
<?php
// Select
// Get single value:
$query = \Drupal::database()->select('node_field_data', 'n');
$query->addField('n', 'nid');
$query->condition('n.title', 'About Us');
$query->range(0, 1);
$nid = $query->execute()->fetchField();
@levmyshkin
levmyshkin / DrupalbookExamplesSubscriber.php
Last active November 19, 2021 14:12
Drupal Event Dispatcher redirect programmatically
<?php
// modules/custom/drupalbook_examples/src/EventSubscriber/DrupalbookExamplesSubscriber.php
namespace Drupal\drupalbook_examples\EventSubscriber;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DrupalbookExamplesSubscriber implements EventSubscriberInterface {