简体   繁体   English

如何以编程方式在Drupal 8中创建实体引用字段?

[英]How to create an entity reference field in Drupal 8 programmatically?

I want to create an entity reference field in Drupal 8 using my custom module. 我想使用我的自定义模块在Drupal 8中创建一个实体引用字段。 Like when someone clicks a link on a Drupal page, an entity reference field would be created in the page node type automatically. 就像有人点击Drupal页面上的链接一样,将自动在页面节点类型中创建实体引用字段。

Can anyone help me with this? 谁能帮我这个?

This will create an entity reference field for node content types with the article bundle. 这将为包含article包的node内容类型创建实体引用字段。

$form['node_id'] = array(
    '#type' => 'entity_autocomplete',
    '#title' => $this->t('Node'),
    '#target_type' => 'node',
    '#selection_settings' => ['target_bundles' => ['article']],
    '#tags' => TRUE,
    '#size' => 30,
    '#maxlength' => 1024,
  );

Note you can change target_type for other entities like taxonomy_term . 请注意,您可以更改taxonomy_term等其他实体的target_type

$node = new stdClass();
$node->title = "YOUR TITLE";
$node->type = "YOUR_NODE_TYPE";
 ....
$node->field_customer_nid[$node->language][]['target_id'] =    $form_state['values']['entity id'];


 ...
 node_submit($node);
 node_save($node);

So I can hopefully find this faster if I need to again... 所以,如果我需要再次,我希望能更快地找到它...

Create field storage: 创建字段存储:

 if (!$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) {
   FieldStorageConfig::create([
     'field_name' => $field_name,
     'entity_type' => $entity_type,
     'type' => $type,
     'cardinality' => -1,

     // Optional to target entity types.
     'settings' => [
       'target_type' => $entity_target_type, // Ex: node, taxonomy_term.
     ],
   ])->save();
 }

Create field: 创建字段:

FieldConfig::create([
  'field_name' => $field_name,
  'entity_type' => $entity_type,
  'bundle' => $bundle,
  'label' => $label,
  'cardinality' => -1,

  // Optional to target bundles.
  'settings' => [
    'handler' => 'default',
    'handler_settings' => [
      'target_bundles' => [
        $vid1,
        $vid2,
      ],
    ],
  ],
])->save();

You can get help from https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Plugin!DataType!EntityReference.php/8 您可以从https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Plugin!DataType!EntityReference.php/8获得帮助

Or you can download sample entity examples too https://www.drupal.org/project/examples 或者您也可以下载示例实体示例https://www.drupal.org/project/examples

Or you can use my method too which is as below: 或者您也可以使用我的方法,如下所示:

Set up module content_entity_example.info.yml 设置模块content_entity_example.info.yml

name: Content Entity Example
type: module
description: 'Provides ContentEntityExampleContact entity.'
package: Example modules
core: 8.x
# These modules are required by the tests, must be available at bootstrap time
dependencies:
  - options
  - entity_reference
  - examples

Creating a content entity type in Drupal 8 在Drupal 8中创建内容实体类型

We will create a 'Contact' entity to add, edit and delete People (Contacts). 我们将创建一个“联系”实体来添加,编辑和删除人员(联系人)。 It is fully fieldable and uses most of the new entity concepts available in Drupal 8. 它完全可现场使用,并使用Drupal 8中提供的大多数新实体概念。

Routing content_entity_example.routing.yml 路由content_entity_example.routing.yml

# This file brings everything together. Very nifty!

# Route name can be used in sevaral place (links, redirects, local actions etc.)
entity.content_entity_example_contact.canonical:
  path: '/content_entity_example_contact/{content_entity_example_contact}'
  defaults:
  # Calls the view controller, defined in the annotation of the contact entity
    _entity_view: 'content_entity_example_contact'
    _title: 'Contact Content'
  requirements:
  # Calls the access controller of the entity, $operation 'view'
    _entity_access: 'content_entity_example_contact.view'

entity.content_entity_example_contact.collection:
  path: '/content_entity_example_contact/list'
  defaults:
  # Calls the list controller, defined in the annotation of the contact entity.
    _entity_list: 'content_entity_example_contact'
    _title: 'Contact List'
  requirements:
  # Checks for permission directly.
    _permission: 'view contact entity'

content_entity_example.contact_add:
  path: '/content_entity_example_contact/add'
  defaults:
  # Calls the form.add controller, defined in the contact entity.
    _entity_form: content_entity_example_contact.add
    _title: 'Add Contact'
  requirements:
    _entity_create_access: 'content_entity_example_contact'

entity.content_entity_example_contact.edit_form:
  path: '/content_entity_example_contact/{content_entity_example_contact}/edit'
  defaults:
  # Calls the form.edit controller, defined in the contact entity.
    _entity_form: content_entity_example_contact.edit
    _title: 'Edit Contact'
  requirements:
    _entity_access: 'content_entity_example_contact.edit'

entity.content_entity_example_contact.delete_form:
  path: '/contact/{content_entity_example_contact}/delete'
  defaults:
    # Calls the form.delete controller, defined in the contact entity.
    _entity_form: content_entity_example_contact.delete
    _title: 'Delete Contact'
  requirements:
    _entity_access: 'content_entity_example_contact.delete'

content_entity_example.contact_settings:
  path: 'admin/structure/content_entity_example_contact_settings'
  defaults:
    _form: '\Drupal\content_entity_example\Form\ContactSettingsForm'
    _title: 'Contact Settings'
  requirements:
    _permission: 'administer contact entity'

The route names for actions defined in the 'link' section of the entity annotation must follow the right pattern. 实体注释的“链接”部分中定义的操作的路径名称必须遵循正确的模式。 For details please see the Content Entity Class below. 有关详细信息,请参阅下面的内容实体类。

content_entity_example.links.menu.yml content_entity_example.links.menu.yml

In combination with the routing file, this replaces hook_menu for the module. 结合路由文件,这将替换模块的hook_menu。

# Define the menu links for this module

entity.content_entity_example_contact.collection:
  title: 'Content Entity Example: Contacts Listing'
  route_name: entity.content_entity_example_contact.collection
  description: 'List Contacts'
  weight: 10
content_entity_example_contact.admin.structure.settings:
  title: Contact Settings
  description: 'Configure Contact entity'
  route_name:  content_entity_example.contact_settings
  parent: system.admin_structure

content_entity_example.links.action.yml content_entity_example.links.action.yml

# All action links for this module

content_entity_example.contact_add:
  # Which route will be called by the link
  route_name: content_entity_example.contact_add
  title: 'Add Contact'

  # Where will the link appear, defined by route name.
  appears_on:
    - entity.content_entity_example_contact.collection
    - entity.content_entity_example_contact.canonical

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM