Am new to Drupal and this week I learned quite a bit about module development, and for today the case is:
For my new module, I create a custom block that renders a markup, and from the folder structure, the class looks like this:
Note: fax is the module's machine name.
fax/src/Plugin/Block/FaxBlock.php
<?php
namespace Drupal\fax\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
/**
* Provides a 'Fax' block.
*
* @Block(
* id = "fax_block",
* admin_label = @Translation("Fax block"),
* )
*/
class FaxBlock extends BlockBase implements BlockPluginInterface {
public function build() {
return array(
'#markup' => $this->t('This is just a text initially'),
);
}
}
I know that in the build method we can also return a form for instance:
public function build() {
return \Drupal::formBuilder()->getForm('Drupal\rsvplist\Form\RSVPForm');
}
And that we can put HTML inside the array if we want.
Question: how can I instead create a template a return that inside the array?
so, instead of returning a markup like this:
'#markup' => $this->t('This is just a text initially'),
I could have a template somewhere:
<div>
<p>This is a great example</p>
</div>
and then return the template in the build function.
How can I return a template inside the build method of my block?
hook_theme()