drupal8 学习笔记 - 创建block

Drupal 8 中的块 API 变了,现在块既是插件(plugins)也是实体(entities)。

#Anna@bogon~/Sites/vagrant-np7/drupal8/sites/all/modules/examples/block_example (master) ☀️✨$ ls
block_example.info.yml  src
#drupal8/sites/all/modules/examples/block_example/src/Plugin/Block/ExampleConfigurableTextBlock.php
 $this->t('AAAAAA default value. This block was created at %time', ['%time' => date('c')]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['block_example_string_text'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Block contentsA'),
      '#size' => 60,
      '#description' => $this->t('This text will appear in the example block.'),
      '#default_value' => $this->configuration['block_example_string'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['block_example_string']
      = $form_state->getValue('block_example_string_text');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#type' => 'markup',
      '#markup' => $this->configuration['block_example_string'],
    ];
  }

}

注意blockForm,blockSubmit,build这些方法。
build是呈现block列表,markup获取block_example_string 里面的内容显示,blocksubmit获取block_example_string_text内容提交到block_example_string,blockform是各种表单元素,defaultConfigure指默认数据。
admin_label是block的默认title,category选block的时候可以看见。

写完激活,添加到region中,在页面中就可以显示。

你可能感兴趣的:(drupal8 学习笔记 - 创建block)