Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Drupal 8表单元素,带有#自动创建分类术语_Php_Drupal_Drupal 8 - Fatal编程技术网

Php Drupal 8表单元素,带有#自动创建分类术语

Php Drupal 8表单元素,带有#自动创建分类术语,php,drupal,drupal-8,Php,Drupal,Drupal 8,我用表单制作了模块,它使用自动完成字段,如下所示: $form['field_taxonomy_tags'] = [ '#type' => 'entity_autocomplete', '#target_type' => 'taxonomy_term', '#selection_settings' => [ 'target_bundles' => array('tags'), ],

我用表单制作了模块,它使用自动完成字段,如下所示:

    $form['field_taxonomy_tags'] = [
       '#type' => 'entity_autocomplete',
       '#target_type' => 'taxonomy_term',
       '#selection_settings' => [
           'target_bundles' => array('tags'),
       ],
        '#autocreate' => array(
          'target_bundles' => array('tags'),
          'bundle' => ('tags'),
        ),
       '#title' => ('tags'),
        '#tags' => TRUE,
     ];
Autocomplete工作得很好,我可以很容易地从标记词汇表中添加分类术语。但我认为“自动创建”选项存在一些问题。搜索了drupal核心中的所有文档和代码。实体永远不会被创造/ 当我试图从该字段中获取值时,我的浏览器已死机。。。有一些实体类型变量,但很大

经过一些调试,我找到了让它工作的方法,但我不高兴:)很奇怪,也许你们中的一些人可以帮我找到更好的方法

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $tags = $form_state->getValue('field_taxonomy_tags');
    foreach ($tags as $tag)
    {
        if(is_object($tag['entity']))
        {
            $tag['entity']->save();
        }
    }
  }

如您所见,我需要手动保存这些标记,不知道为什么;/没有它,就没有术语产生。

这是更好的方法。我不需要保存每个标记,如果我们将它们附加到一个节点上就足够了。其实体对象,可作为节点值传递,之后将创建所有标记:

  $node = Node::create(array(
      'type' => 'YOUR_content_type',
      'title' => $form_state->getValue('title')
  ));
  $fieldNames = array_keys($node->getFieldDefinitions());
  $values = $form_state->getValues();
  // be aware with that, i use this loop for testing because i have same names
  // you can use $node->set('content type field name', $value); directly without any field definitions
  foreach ($values as $key=>$value)
  {
      if(in_array($key, $fieldNames))
      {
          $node->set($key, $value);
      }
  }
  // here we save all data, taxonomy entities too
  $node->save();