bundleFieldDefinitions example

->setRevisionable(FALSE)
      ->setTranslatable(FALSE);

    $fields += \Drupal::state()->get('entity_test_update.additional_base_field_definitions', []);
    return $fields;
  }

  /** * {@inheritdoc} */
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type$bundle, array $base_field_definitions) {
    $fields = parent::bundleFieldDefinitions($entity_type$bundle$base_field_definitions);
    $fields += \Drupal::state()->get('entity_test_update.additional_bundle_field_definitions.' . $bundle[]);
    return $fields;
  }

}

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields['name']->setDescription('The default description.');
    return $fields;
  }

  /** * {@inheritdoc} */
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type$bundle, array $base_field_definitions) {
    $fields = parent::bundleFieldDefinitions($entity_type$bundle$base_field_definitions);

    if ($bundle == 'some_test_bundle') {
      $fields['name'] = clone $base_field_definitions['name'];
      $fields['name']->setDescription('Custom description.');
    }
    return $fields;
  }

}

  protected function buildBundleFieldDefinitions($entity_type_id$bundle, array $base_field_definitions) {
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);

    // Use a bundle specific class if one is defined.     $class = $this->entityTypeManager->getStorage($entity_type_id)->getEntityClass($bundle);

    // Allow the entity class to provide bundle fields and bundle-specific     // overrides of base fields.     $bundle_field_definitions = $class::bundleFieldDefinitions($entity_type$bundle$base_field_definitions);

    // Load base field overrides from configuration. These take precedence over     // base field overrides returned above.     $base_field_override_ids = array_map(function D$field_name) use ($entity_type_id$bundle) {
      return $entity_type_id . '.' . $bundle . '.' . $field_name;
    }array_keys($base_field_definitions));
    $base_field_overrides = $this->entityTypeManager->getStorage('base_field_override')->loadMultiple($base_field_override_ids);
    foreach ($base_field_overrides as $base_field_override) {
      /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $base_field_override */
      $field_name = $base_field_override->getName();
      $bundle_field_definitions[$field_name] = $base_field_override;
    }

  public static $bundleClassFieldDefinitions = [];

  /** * {@inheritdoc} */
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type$bundle, array $base_field_definitions) {
    $definitions = parent::bundleFieldDefinitions($entity_type$bundle$base_field_definitions);

    if (isset(static::$bundleClassFieldDefinitions[$entity_type->id()][$bundle])) {
      $definitions += static::$bundleClassFieldDefinitions[$entity_type->id()][$bundle];
    }

    return $definitions;

  }

}
Home | Imprint | This part of the site doesn't use cookies.