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 | |
$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 | |
// 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 | |
//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 | |
// 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 | |
// 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 | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function drupalbook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { | |
if ($form_id == 'drupalbook_admin_settings') { | |
$form['drupalbook_api_key']['#attributes']['placeholder'] = 'API key'; | |
$form['drupalbook_api_client_id']['#attributes']['placeholder'] = 'API client 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 | |
// /modules/custom/drupalbook/src/Form/DrupalbookSettingsForm.php | |
namespace Drupal\drupalbook\Form; | |
use Drupal\Core\Form\ConfigFormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Configure example settings for this site. | |
*/ |
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/Controller/DisplayNode.php | |
namespace Drupal\drupalbook\Controller; | |
use Drupal\Core\Access\AccessResult; | |
use Drupal\node\NodeInterface; | |
/** | |
* Provides route responses for the DrupalBook module. |
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 | |
/** | |
* @file /modules/custom/drupalbook/src/Controller/FirstPageController.php | |
*/ | |
namespace Drupal\drupalbook\Controller; | |
/** | |
* Provides route responses for the DrupalBook module. |