This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load node by nid: | |
$nid = 234; | |
$node_storage = \Drupal::entityTypeManager()->getStorage('node'); | |
$node = $node_storage->load($nid); | |
// Get node id: | |
$nid = $node->id(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$my_paragraph = null; | |
foreach ($node->get('field_paragraph_reference') as $paragraph) { | |
if ($paragraph->entity->getType() == 'your_paragraph_type') { | |
$my_paragraph = $paragraph->entity; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create node programmatically. | |
use \Drupal\node\Entity\Node; | |
$node = Node::create([ | |
'type' => 'article', | |
'title' => 'Druplicon test', | |
]); | |
$node->save(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |