【问题标题】:Drupal 8 how to save and render an image using a custom block configurationDrupal 8 如何使用自定义块配置保存和渲染图像
【发布时间】: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


    【解决方案1】:

    当您保存该表单时,图像字段中的值就是文件 ID。

    因此,您可以通过以下方式获取文件对象和路径:

    $image = \Drupal\file\Entity\File::load($fid);
    $path = file_create_url($image->getFileUri());
    

    然后您将使用标记变量中的路径输出图像。可能有一种更语义化的方式以“Drupal-ish”形式输出格式化图像,但这会让你开始。

    哦,还有 body 字段,使用

    $form_state->getValue('body')['value'];
    

    在您的标记中。

    顺便说一句,我喜欢使用 Devel 和 ksm() 函数!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多