0

I have a block as part of my custom module, I was it to show 2 text boxes (Input & Output) and have a generate button. I cant get it to show the input boxes! It seems they are being stripped out for some reason? I have tried with and without twig but my example below is without twig to keep it simple:

    <?php

/**
 * @file
 * Contains \Drupal\mymodule\Plugin\Block\GenerateBlock.
 */

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'generateblock' block.
 *
 * @Block(
 *   id = "generate_block",
 *   admin_label = @Translation("Generate Code"),
 *   category = @Translation("mymodule")
 * )
 */
class generateblock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    // $twig = \Drupal::service('twig');
    // $template = $twig->loadTemplate(drupal_get_path('module', 'mymodule') . '/templates/generatecode.html.twig');
    // $output = $template->render([]);
    $output='<div id="GenerateCodeBox" class="featured-boxes featured-boxes-style-8">
    <div class="col-md-12">
    <div class="featured-box featured-box-primary featured-box-text-left" style="height: 301px;">
    <div class="box-content">
    <div class="row">
    <div class="col-md-9">
    <h2>Generate <strong>Access Code</strong></h2>
    </div>

    <div class="col-md-3">
    <div class="align-right">
    <h2><i class="fa fa-twitter">‌</i></h2>
    </div>
    </div>
    </div>

    <div class="row">
    <div class="col-md-12">
    <div class="input-group"> 
    <input class="form-control" id="codeToCheck" placeholder="Enter Input" type="text" value="" /> <span class="input-group-btn" id="generateMe"><button class="btn btn-default mr-xs mb-sm" type="button">Set</button>
    </span>
    </div>

    <div class="input-group"><input class="form-control" id="validationCode" placeholder="" readonly="readonly" type="text" value="" /> <span class="input-group-btn" id="copyCode"><button class="btn btn-default mr-xs mb-sm" type="button">Copy</button></span></div>
    <a class="btn btn-lg btn-primary mr-xs mb-lg" href="#" type="button">Learn More</a></div>
    </div>
    </div>
    </div>
    </div>
    </div>';

    return array(
      '#type' => 'markup',
      '#markup' => $output,
    );
  }

}

I do not know why the inputs are being stripped out? What am i missing

**** Update ****

I have changed my code to the following and it is no longer stripping out the input boxes but it feels a bit "hacky" Do I need to specify a different render type or something:

class AccessCode extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $twig = \Drupal::service('twig');
    $template = $twig->loadTemplate(drupal_get_path('module', 'mymodule') . '/templates/accesscode.html.twig');
    $output = $template->render([]);

    return array(
      '#markup' => $output,
      '#allowed_tags' => ['input','span','div','a','button','h2','strong','i',], 
    );
  }
1

try to follow best practises: as no need to render anything on your get the twig template as drupal core handles this.

module structure:

mymodule

  • mymodule.module
  • mymodule.info.yml
  • mymodule.libraries.yml
  • templates (dir) -- templates/accesscode.html.twig
  • src (dir) -- src/Plugin/Block ----- AccessCode.php

in mymodule.module

/**
 * Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path) {
    return [
      'accesscode_block' => [
        'variables' => [
          'test' => null,
         ],
         'template' => 'accesscode', 
//no need for full accesscode.html.twig as drupal knows. 
      ],
    ];
} 

then in your src/Plugin/Block/ AccessCode.php

 /**
 * {@inheritdoc}
 */
public function build() {
  $build = [];
  $build['my-block'] = [
      '#theme' => 'accesscode_block',
      '#test' => 1234,
  ];
  // Attach js or css easier. 
  $build['#attached']['library'][] = 'mymodule/mymodule-accesscode';

// Could even add a form element like ..
 $build['some_field'] = [
   '#type' => 'textfield',
   '#title' => 'My Field:',
 ];
  return $build;
}

then in your accesscode.html.twig you can use the twig var {{test}}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.