【问题标题】:File::load in drupal8 custom module controller文件::加载 drupal8 自定义模块控制器
【发布时间】:2017-07-04 05:09:04
【问题描述】:

我在控制器文件中创建了一个自定义模块,我使用了 File::load 静态方法。但是当我运行 phpcs 来检查编码标准时,它会给出一个错误

应避免在类中调用 File::load,改用依赖注入

任何人都可以请告诉如何为此创建依赖注入。

【问题讨论】:

    标签: drupal-modules drupal-8


    【解决方案1】:

    使用 Drupal\Core\Entity\EntityTypeManagerInterface 来实现。

    use Drupal\Core\Entity\EntityTypeManagerInterface;
    
    class MyForm extends FormBase {
    
     /**
      * The storage handler class for files.
      *
      * @var \Drupal\file\FileStorage
      */
     protected $fileStorage;
    
     /**
      * This is an example.
      *
      * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity
      *   The Entity type manager service.
      */
      public function __construct(EntityTypeManagerInterface $entity) {
        $this->fileStorage = $entity->getStorage('file');
      }
      ....
     }
    

    您可以从那里将File::load($fid) 更新为$this->fileStorage->load($fid)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多