2015-04-15 5 views

risposta

-1

È possibile ottenere aiuto da https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Plugin!DataType!EntityReference.php/8

Oppure si può scaricare esempi di entità campione troppo https://www.drupal.org/project/examples

Oppure si può utilizzare il mio metodo troppo che è il seguente:

impostare il modulo 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 

Creazione di un tipo di entità di contenuto in Drupal 8

Creeremo un'entità 'Contatto' per aggiungere, modificare ed eliminare People (Contatti). È completamente fieldable e utilizza la maggior parte dei nuovi concetti di entità disponibile in Drupal 8.

Routing 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' 

I nomi di percorso per le azioni definite nella sezione 'link' di l'annotazione dell'entità deve seguire lo schema corretto. Per i dettagli si prega di consultare la Content Entity Class di seguito.

content_entity_example.links.menu.yml

In combinazione con il file di routing, questo hook_menu sostituisce per il modulo.

# 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

# 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 
+0

Questo sembra essere il codice per la creazione di un tipo di entità personalizzata? Ma la domanda riguarda la creazione di un campo di riferimento di entità. – Christian

0
$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); 
+0

Si potrebbe voler aggiungere del testo di accompagnamento a questo. Non è troppo chiaro ciò che stai dimostrando. – Christian

0

Ciò creerà un campo di riferimento di entità per node tipi di contenuto con il article fascio.

$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, 
); 

Nota È possibile modificare target_type per altre entità come taxonomy_term.

0

Quindi posso spera trovare questo più velocemente se ho bisogno di nuovo ...

Crea stoccaggio campo:

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(); 
} 

Crea campo:

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(); 
+0

hai trovato documentazione ufficiale sul tuo codice sopra? Mostrando i valori chiave? Si noti che le "impostazioni" mapperanno (in una certa misura) i nomi dei campi del modulo trovati nella pagina delle impostazioni. Quindi, per avere una casella di controllo per un dato tipo di contenuto; ''page' => 'page''. – zkolnik

+0

Non sono riuscito a trovare molta documentazione online, il che è uno dei motivi principali per cui ho postato qui. Tuttavia il modulo di base del core ne ha un buon esempio nella classe 'EntityReferenceAutoCreateTest'. L'esempio usa anche "target_bundles" e "target_type". Inoltre, se guardi la pagina delle impostazioni anche le caselle di controllo. – sareed