Module to create a custom field in all the content types I am building a module to insert a custom field in all bundles of the node. I have encountered many difficulties, any help they can give me, I would appreciate it very much.

The custom field I am creating is used to save a tocken automatically or manually. - field_custom_tocken -

If a new bundle is created, the field_custom_tocken field must be added automatically to the new bundle. To generate the token it is necessary the id of the entity (nid), or that the tockent is inserted by the user.

I have used Drupal Console, create the field type, the widget and the formatter. This does not generate the field, the drawback is that you have to manually add the field to the bundle.
drupal generate: plugin: field

In this documentation the field is created and added to a specific bundle, I use part of this documentation. field.storage.node.field_brand_information.yml 

I have this:

this creates the field.
modules/custom/custom_tocken/config/install/field.storage.node.field_custom_tocken.yml

# field.storage.node.field_custom_tocken.yml
langcode: en
status: true
dependencies:
  enforced:
    module:
    - custom_tocken
  module:
    - node
    - text
id: node.field_custom_tocken
field_name: field_custom_tocken
entity_type: node
type: string
settings: {  }
module: core
locked: true
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false


This should create the custom field in the existing bundles, when the module is installed, but it does not work :(

/**
 * Implements hook_install().
 */
function ide_d8_url_shortener_install() {
  $entity_type         = 'node';
  $bundle              = '';
  $field['field_name'] = 'field_custom_tocken';
  $field['label']      = 'Tocken';
  $field['entity_type']= 'node';
  $field['type']       = 'string';
  $field['settings']   = array();

  $bundles = \Drupal::service('entity_type.bundle.info')-&gt;getBundleInfo($entity_type);
  $arr_bundles = [];

  foreach($bundles as $key =&gt; $value) {
    $bundle = $key;
    $fields       = \Drupal::entityManager()-&gt;getFieldDefinitions($entity_type, $bundle);

    if ( !isset($fields['field_short_url']) ) {
      entity_get_form_display($entity_type, $bundle, 'default')
        -&gt;setComponent($field['field_name'], $field )
        -&gt;save();

      entity_get_display($entity_type, $bundle, 'default')
        -&gt;setComponent($field['field_name'], $field)
        -&gt;save();
    }
  }
}


Any help you can give me I would appreciate it.
Instead of installing the field in your install hook (which will be triggered when installing the module only), you should implement hook_entity_base_field_info() to add the field to all nodes and all bundles.

An example below:

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Implements hook_entity_base_field_info().
 *
 * Attaches a custom field 'field_custom_tocken' to all nodes.
 */
function mymodule_entity_base_field_info(EntityTypeInterface $entity_type) {
  // Whether the entity type is 'node'.
  if ($entity_type-&gt;id() == 'node') {
    return [
      'field_custom_tocken' =&gt; BaseFieldDefinition::create('string')
        -&gt;setName('field_custom_tocken')
        -&gt;setLabel(t('Tocken'))
        -&gt;setDescription(t('My tocken, which might be named token instead.'))
        -&gt;setSettings([
          'max_length' =&gt; 150,
          'text_processing' =&gt; 0,
        ])
        -&gt;setDefaultValue('')
        -&gt;setDisplayOptions('view', [
          'label' =&gt; 'above',
          'type' =&gt; 'string',
          'settings' =&gt; [
            'link_to_entity' =&gt; FALSE,
          ],
          'weight' =&gt; 0,
        ])
        -&gt;setDisplayOptions('form', [
          'type' =&gt; 'string_textfield',
          'weight' =&gt; 0,
        ])
        -&gt;setDisplayConfigurable('form', TRUE)
        -&gt;setDisplayConfigurable('view', TRUE),
    ];
  }
}