【发布时间】:2018-02-23 20:42:12
【问题描述】:
我正在尝试创建一个带有配置的自定义模块,该模块将允许一个块具有自定义字段。我在允许上传图片然后在网站上的块中呈现它时遇到问题。
目前这是我的块文件的样子;
使用 Drupal\Core\Block\BlockBase; 使用 Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'hello' block.
*
* @Block(
* id = "hello_block",
* admin_label = @Translation("Hello"),
* category = @Translation("Hello world block")
* )
*/
class HelloBlock extends BlockBase
{
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $formState)
{
$form['heading'] = array(
'#type' => 'textfield',
'#title' => t('Heading'),
'#description' => t('Enter the main heading'),
'#default_value' => 'Main heading'
);
$form['sub_heading'] = array(
'#type' => 'textfield',
'#title' => t('Sub heading'),
'#description' => t('Enter the sub heading'),
'#default_value' => 'Sub heading'
);
$form['body'] = array(
'#type' => 'text_format',
'#title' => t('Body'),
'#description' => t('Main body'),
'#format' => 'full_html',
'#rows' => 50,
'#default_value' => ''
);
$form['image'] = array(
'#type' => 'managed_file',
'#upload_location' => 'public://upload/hello',
'#title' => t('Image'),
'#upload_validators' => [
'file_validate_extensions' => ['jpg', 'jpeg', 'png', 'gif']
],
'#default_value' => isset($this->configuration['image']) ? $this->configuration['image'] : '',
'#description' => t('The image to display'),
'#required' => true
);
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $formState)
{
$this->configuration['heading'] = $formState->getValue('heading');
$this->configuration['sub_heading'] = $formState->getValue('sub_heading');
$this->configuration['body'] = $formState->getValue('body');
$this->configuration['image'] = $formState->getValue('image');
}
/**
* {@inheritdoc}
*/
public function build()
{
$markup = '<h1>'.$this->configuration['heading'].'</h1>';
$markup .= '<h2>'.$this->configuration['sub_heading'].'</h2>';
$markup .= '<img src="'.$this->configuration['image']['value'].'">';
$markup .= '<div>' . $this->configuration['body'] . '</div>';
return array(
'#type' => 'markup',
'#markup' => $markup,
);
}
}
谁能提供一些关于为什么图像没有出现的指示?我假设我错过了一些东西。
保存在正文(text_format)中的文本也以“数组”的形式出现在网站的块中,如果有人也能提供帮助,那就太好了,否则我会提出另一个问题。
【问题讨论】:
标签: drupal-8