【问题标题】:Dependencies for custom forms in Drupal 7Drupal 7 中自定义表单的依赖项
【发布时间】:2018-03-01 22:05:56
【问题描述】:

我在 Drupal 7 中有多个针对单一内容类型的表单。这样做的目的是在用户提交表单时启动不同的工作流程,具体取决于包含的信息类型,并由 /url 为每个表单定义。这些表单位于不同的页面上,每个页面上显示的字段都在自定义模块中定义。例如:

.../form1 启动工作流 1 并显示字段 a、b、e、f、g

.../form2 启动工作流 2 并显示字段 a、b、c、e、h

.../form3 启动工作流 3 并显示字段 a、b、f、x、y

在这个模块中它看起来像这样:

function my_custom_module_custom_form() {
// Build Form
  $form = getForm('content_type');

    switch (strtolower($form['#action'])):
      case('/form1'):  
        $form['field_some_field']['#access'] = FALSE;

    switch (strtolower($form['#action'])):
      case('/form2'):
        $form['field_other_field']['#access'] = FALSE;

我希望每个表单都有一个页面模板,因此我可以指定每个表单的内容,而不是显示/隐藏模块中每个字段的每个字段,考虑到字段的数量,这很麻烦。

我可以为每个表单创建一个页面模板并链接提交按钮以触发模块中的特定操作吗?

注意:添加依赖项或使用单独的内容类型不适用于我们的用例。如果上面的代码有错误,只是我在这里给出了一个简单的例子,实际的模块可以工作。

感谢您的帮助!

【问题讨论】:

  • 你用过不同的表格吗?如果是,那么它具有不同的表单 ID,然后是为每个表单创建模板文件的最佳方法,以便您可以根据需要呈现特定字段。

标签: forms drupal-7 drupal-forms drupal-form-submission


【解决方案1】:

我已经为特定节点表单创建了主题建议:

Change the function name to THEMENAME_preprocess_node
Change initial value of template_filename to 'node'
Account for dashes in aliases by adding this line below the second if statement: $alias = str_replace('-', '_', $alias);

这就是它现在的样子:

function THEMENAME_preprocess_node(&$variables, $hook) {
  // Node template suggestions based off URL alias
  if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $alias = str_replace('-', '_', $alias);
      $template_filename = 'node';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '__' . $path_part;
        $variables['theme_hook_suggestions'][] = $template_filename;
      }
    }
  }
}

如果您需要更多详细信息,请告诉我。

【讨论】:

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