I have created a module that let the user to add a title and a body to a form and on submit, a new article is added. I need to display this newly added article’s title in a block. How can I do it?
<?php
namespace Drupalexform1Form;
use DrupalCoreFormFormBase;
use DrupalCoreFormFormStateInterface;
use DrupalnodeEntityNode;
class SimpleCustomForm extends FormBase {
public function getFormId() {
return 'simple_custom_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $username = NULL) {
$form['title'] = [
'#type' => 'textfield',
'#title' => 'Title:',
'#required' => TRUE,
];
$form['body'] = [
'#type' => 'textfield',
'#title' => 'Body:',
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Create node'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = array(
'nid' => NULL,
'type' => 'article',
'title' => $form_state->getValue('title'),
'body' => $form_state->getValue('body'),
'uid' => 1,
'status' => TRUE,
);
entity_create('node', $values)->save();
drupal_set_message(t('Created new node'));
}
}```