Ced
Ced

Reputation: 199

Creating Configuration Entities with entity reference in Drupal 8

In drupal 8, is it possible to create complex configuration entities with reference entities? For example, to link multiple node in a configuration entities field? I haven't found documentation...

Upvotes: 0

Views: 1460

Answers (1)

Kevin Wenger
Kevin Wenger

Reputation: 1514

I'm not sure I understand your question but I will give you some clues to resolve your issue.

Sorry if my answers doesn't fit you needs.

Do you want only a configuration field linking multiple nodes ?

or

Do you want a custom Configuration Entity ?


Configuration field

Use the field entity_autocomplete would help you. Extra documentation here: https://www.drupal.org/node/2418529

Usage:

/**
* @file
* Contains A custom Class Form.
*/

// ... Namespaces & uses.

class AdminForm extends FormBase {

    // Class definitions

    public function buildForm(array $form, FormStateInterface $form_state, $extra = NULL) {
        $form['node_collection'] = array(
            '#type'          => 'entity_autocomplete',
            '#target_type'   => 'node',
            '#title'         => 'Nodes',
        );
    }
}

You should then save it in the State or the Config API.
Check this article for the difference between both (it will help you to choose the correct one): https://antistatique.net/en/we/blog/2016/06/14/drupal-8-differences-between-configuration-api-state-api


Configuration entity

You should take a look at this documentation about Configuration Entity: https://www.drupal.org/docs/8/api/configuration-api/creating-a-configuration-entity-type-in-drupal-8

Then, you could look this answer, It will help you to create an entity_reference field for node or any entity you want. Retrieve a taxonomy term in the buildrow function of a drupal 8 custom entity

Hope it will help you !

Upvotes: 2

Related Questions