【问题标题】:How to generate files with a custom module and PHP?如何使用自定义模块和 PHP 生成文件?
【发布时间】:2021-07-10 01:57:52
【问题描述】:

我有一个使用 Drupal 8 的站点。我创建了一个自定义模块。这是它的代码:

https://github.com/S1BIOSE/generator_website

generator-website-page.html.twig:

<div class="card mb-5 overflow-hidden shadow rounded bg-white">
  <div class="card-body">
   
    <form>

      <legend>Générateur de site web</legend>
      <div class="mb-3">
        <label for="TokenUrl" class="form-label">L'URL de votre site web</label>
        <input type="text" class="form-control is-invalid" id="TokenUrl" required>
        <div class="invalid-feedback">
          Ce champ est requis.
        </div>
        <small id="helpUrl" class="form-text">Entrez l'URL complète de votre site web.</small>
      </div>
      <div class="mb-3">
        <label for="TokenTitle" class="form-label">Nom de l'entreprise</label>
        <input type="text" class="form-control is-invalid" id="TokenTitle" required>
        <div class="invalid-feedback">
          Ce champ est requis.
        </div>
        <small id="helpTitle" class="form-text">Entrez le nom de votre entreprise.</small>
      </div>
      <div class="mb-3">
        <label for="TokenDescription" class="form-label">Présentation de l'entreprise</label>
        <textarea class="form-control is-invalid" id="TokenDescription" rows="5" required></textarea>
        <div class="invalid-feedback">
          Ce champ est requis.
        </div>
        <small id="helpDescription" class="form-text">Entrez une description de votre entreprise.</small>
      </div>

      <div class="mb-3">
        <label for="TokenFeed" class="form-label">Fil d'actualité</label>
        <input type="text" class="form-control" id="TokenFeed">
        <small id="helpFeed" class="form-text">Entrez l'url de votre Flux RSS sur la plateforme S1BIOSE.</small>
      </div>

      <button type="submit" class="btn btn-primary">Générer</button>
    </form>

  </div>
</div>

我应该在我的模块中放入什么来生成上面的3个文件并替换以 Token 开头的单词?

在这个自定义模块中,我创建了一个带有 ID 的表单(我不确定这是否是正确的方法)。例如,在 TokenTitle 字段中输入的数据必须替换 TokenTitle 出现在文件中的任何位置。

当用户提交表单时,它必须下载包含正确数据的 3 个文件(在表单中输入的那个)。如果可能,在 ZIP 存档中。

表格中提交的信息无需保存在数据库中。

manifest.json

{
  "orientation":"portrait",
  "short_name": "TokenTitle",
  "name": "TokenTitle",
  "display": "standalone",
  "background_color": "#000000",
  "theme_color": "#000000",
  "description": "TokenDescription",
  "lang": "fr",
  "icons": [{
        "src": "icon-144.png",
        "sizes": "144x144",
        "type": "image/png",
        "purpose": "any maskable"
      }, {
        "src": "icon-192.png",
        "sizes": "192x192",
        "type": "image/png",
        "purpose": "any maskable"
      }, {
        "src": "icon-512.png",
        "sizes": "512x512",
        "type": "image/png",
        "purpose": "any maskable"
      }],
  "start_url": "/?source=pwa",
  "scope": "/"
}

站点地图.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>TokenUrl/index.html</loc>
<lastmod>TokenDate</lastmod>
</url>
</urlset>

sw.js

const staticCacheName = 'TokenTimestamp';
const filesToCache = [
  '/',
  '/index.html',
  '/CHANGELOG.md',
  '/bootstrap.min.css',
  '/style.css',
  '/bootstrap.bundle.min.js',
  '/popover.js',
  '/clipboard.min.js',
  '/btn-clipboard.js',
  '/pwa.js',
  '/feed.js',
  '/toasts.js',
  '/icon-32.png',
  '/icon-144.png',
  '/icon-192.png',
  '/icon-512.png',
  '/iphone5_splash.png',
  '/iphone6_splash.png',
  '/iphoneplus_splash.png',
  '/iphonex_splash.png',
  '/iphonexr_splash.png',
  '/iphonexsmax_splash.png',
  '/ipad_splash.png',
  '/ipadpro1_splash.png',
  '/ipadpro3_splash.png',
  '/ipadpro2_splash.png'
];

self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(staticCacheName).then(cache => {
      return cache.addAll(filesToCache);
    })
  );
});

self.addEventListener('activate', event => {
  event.waitUntil(caches.keys().then(function(cacheNames) {
    return Promise.all(
      cacheNames.filter(function(staticCacheName) {
      }).map(function(staticCacheName) {
        return caches.delete(staticCacheName);
      })
    );
  }));
});

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request).then(cachedResponse => {
      if (cachedResponse) {
        return cachedResponse;
      }
      return fetch(event.request);
    })
  );
});

self.addEventListener('message', event => {
  if (event.data.action === 'skipWaiting') {
    self.skipWaiting();
  }
});

【问题讨论】:

    标签: php file drupal drupal-8 drupal-modules


    【解决方案1】:

    您好,您需要知道如何做:

    1. 了解如何使用 php 制作文件:https://www.w3schools.com/php/php_file_create.asp

    2. 了解 Drupal 路由和控制器的工作原理: https://www.drupal.org/docs/drupal-apis/routing-system/introductory-drupal-8-routes-and-controllers-example

    3. 了解 Drupal 如何处理表单https://www.drupal.org/docs/drupal-apis/form-api/introduction-to-form-api

    一旦你知道了这些事情,你就可以在表单提交中使用 一些 php 来生成你的文件,然后可能将它们分组以供下载......或者你对它们做了什么。

    【讨论】:

    • 听起来很复杂。我的表单在这里(它是使用 Bootstrap 创建的)s1biose.com/fr/generator-website 无法将表单字段的 ID 与文件中以“Token”开头的单词匹配?如果没有,创建令牌不是更容易吗?
    • 正如 taggartJ 所说,您应该使用 Drupals 表单 API 来创建表单 - 因为它可以保护您免受潜在的 XSS 和 CSRF 攻击(以及其他原因)。
    • @Pobtastic 即使我不想将表单保存到数据库中?
    • 这不是将表单保​​存到数据库,而是保护表单不被恶意用户使用。
    【解决方案2】:

    不要使用标准的 PHP 来制作文件,因为那样你就需要处理家务/删除它/管理它。 Drupal 有BinaryFileResponse 用于向用户发送文件,\Drupal\Core\File\FileSystemInterface 用于创建/管理文件。如果是临时的,那么只需设置它as this

    【讨论】:

    • 谢谢,是的,在用户(匿名或注册)下载 ZIP 存档后,我不想保留数据和文件。我是 drupal 自定义模块的新手,我不知道如何使用 PHP。请问我应该在我当前的模组中添加什么?
    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 2011-07-11
    • 2016-12-12
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多