【问题标题】:Create content in Drupal based on options in a drop down根据下拉菜单中的选项在 Drupal 中创建内容
【发布时间】:2012-07-11 16:40:03
【问题描述】:

我正在开发一个需要“职业”页面的 Drupal 网站。我有一份包含 20 多份工作的清单,以及 30 多处可能提供这些工作的地点。

我要做的就是做到这一点,当有工作可用时,需要做的就是用户选择职位名称和可用位置,然后它将创建带有职位描述的帖子以及我拥有的其他信息以及该位置的信息。

我遇到的另一个问题是制作它,所以我可以拥有多个实例......例如。如果两个或多个地点有相同的工作。

我一直在努力思考我将如何完成这项工作,但我一直处于空白状态。如果有人有想法指出我正确的方向,将不胜感激。

【问题讨论】:

    标签: drupal drupal-7 drupal-views content-type


    【解决方案1】:

    听起来像是一个很常见的用例;如果是我,我会这样处理:

    • 创建“工作”内容类型
    • 添加新的“位置”词汇
    • 将“职位”内容类型上的术语参考字段添加到“位置”词汇表中,其值不受限制(或您希望每个职位允许的最大位置数)。
    • 为您的管理员创建一个自定义表单,例如:

      function MYMODULE_add_job_form($form, &$form_state) {
        $form['title'] = array(
          '#type' => 'textfield',
          '#title' => t('Title'),
          '#maxlength' => 255,
          '#required' => TRUE
        );
      
        // Load the vocabulary (the machine name might be different).
        $vocabulary = taxonomy_vocabulary_machine_name_load('location');
      
        // Get the terms
        $terms = taxonomy_get_tree($vocabulary->vid);
      
        // Extract the top level terms for the select options
        $options = array();
        foreach ($terms as $term) {
          $options[$term->tid] = $term->name;
        }
      
        $form['locations'] = array(
          '#type' => 'select',
          '#title' => t('Locations'),
          '#options' => $options,
          '#multiple' => TRUE,
          '#required' => TRUE
        );
      
        $form['submit'] = array(
          '#type' => 'submit',
          '#value' => t('Add job')
        );
      
        return $form;
      }
      
    • 为表单创建自定义提交处理程序以编程方式添加新节点:

      function MYMODULE_add_job_form_submit($form, &$form_state) {
        $location_tids = array_filter($form_state['values']['locations']);
      
        $node = new stdClass;
        $node->type = 'job';
        $node->language = LANGUAGE_NONE;
        node_object_prepare($node);
      
        $node->title = $form_state['values']['title'];
        $node->field_location_term_ref[LANGUAGE_NONE] = array();
      
        foreach ($location_tids as $tid) {
          $node->field_location_term_ref[LANGUAGE_NONE][] = array(
            'tid' => $tid
          );
        }
      
        node_save($node);
      
        $form_state['redirect'] = "node/$node->nid";
      }
      

    显然,您需要为该表单添加页面回调,并且可能需要进行一些小的更改(字段名称等),但它应该为您提供一个良好的起点。您还需要在某些时候加载位置分类术语以提取您提到的描述信息...您可以使用 taxonomy_term_load() 来执行此操作。

    【讨论】:

    • 太棒了。非常感谢!这应该能把我带到我需要去的地方。对不起这么简单的问题。出于某种原因,我的大脑无法连接这些点。
    • 没有问题 :) Drupal 猫的皮肤有很多不同的方法,有时很难知道从哪里开始
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多