Skip to content

Instantly share code, notes, and snippets.

View levmyshkin's full-sized avatar

Ivan Abramenko levmyshkin

View GitHub Profile
@levmyshkin
levmyshkin / MultiStepForm.php
Last active November 19, 2021 13:52
Drupal Multistep popup form
<?php
// modules/custom/drupalbook/src/Form/MultiStepForm.php
namespace Drupal\drupalbook\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class MultiStepForm extends FormBase
{
@levmyshkin
levmyshkin / plugin.php
Created November 19, 2021 13:58
Drupal Working with node fields programmatically
<?php
// Load node by nid:
$nid = 234;
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);
// Get node id:
$nid = $node->id();
@levmyshkin
levmyshkin / file.php
Created November 19, 2021 14:00
Drupal Working with file fields programmatically
<?php
//Getting a file by ID:
$fid = 42;
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$file = $file_storage->load($fid);
// Getting the file object from the node field:
$file = $node->field_image->entity;
@levmyshkin
levmyshkin / reference.php
Created November 19, 2021 14:02
Drupal Working with Entity Reference fields programmatically
<?php
// You can get multiple values from reference fields and process them through foreach:
foreach ($node->field_my_entity_reference as $reference) {
print $reference->target_id;
print $reference->entity->title->value;
}
// Modifying an entity reference multiple field:
$nids = [3,4,5,6];
@levmyshkin
levmyshkin / paragraph.php
Created November 19, 2021 14:03
Drupal Working with Paragraph fields programmatically
<?php
$my_paragraph = null;
foreach ($node->get('field_paragraph_reference') as $paragraph) {
if ($paragraph->entity->getType() == 'your_paragraph_type') {
$my_paragraph = $paragraph->entity;
}
}
@levmyshkin
levmyshkin / entity.php
Created November 19, 2021 14:06
Add, update, delete Drupal Entity programmatically
<?php
// Create node programmatically.
use \Drupal\node\Entity\Node;
$node = Node::create([
'type' => 'article',
'title' => 'Druplicon test',
]);
$node->save();
@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 {
@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 / 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 / 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