如何使用自定义模块和PHP生成文件? [英] How to generate files with a custom module and PHP?

查看:62
本文介绍了如何使用自定义模块和PHP生成文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a site with Drupal 8. I have created a custom module. Here is its code :

https://github.com/S1BIOSE/generator_website

generator-website-page.html.twig:

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开头的单词?

What should I put in my module to generate the 3 files above and replace the words starting with Token ?

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

In this custom module, I created a form with IDs (I'm not sure if this is the right way to go). For example, the data entered in the TokenTitle field must replace TokenTitle wherever it appears in the files.

用户提交表单时,必须下载具有正确数据的3个文件(在表单中输入的文件).如果可能,请在ZIP存档中.

When a user submits the form, it must download the 3 files with the correct data (the one entered in the form). If possible in a ZIP archive.

不必要将表单中提交的信息保留在数据库中.

It is unnecessary to keep the information submitted in the form in the database.

manifest.json

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": "/"
}

sitemap.xml

sitemap.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来制作文件,因为那样您就需要处理内务处理/删除/管理文件.Drupal具有

Don't use standard PHP to make the file, as then you'll need to handle housekeeping/ removing it/ managing it as well. Drupal has BinaryFileResponse for sending files to users and \Drupal\Core\File\FileSystemInterface for creation/ management of files. If it is temporary, then simply set it as this.

这篇关于如何使用自定义模块和PHP生成文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆