FieldException example

/** * {@inheritdoc} */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $settings = $field_definition->getSettings();
    $target_type_info = \Drupal::entityTypeManager()->getDefinition($settings['target_type']);

    // If the target entity type doesn't have an ID key, we cannot determine     // the target_id data type.     if (!$target_type_info->hasKey('id')) {
      throw new FieldException('Entity type "' . $target_type_info->id() . '" has no ID key and cannot be targeted by entity reference field "' . $field_definition->getName() . '"');
    }

    $target_id_data_type = 'string';
    if ($target_type_info->entityClassImplements(FieldableEntityInterface::class)) {
      $id_definition = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($settings['target_type'])[$target_type_info->getKey('id')];
      if ($id_definition->getType() === 'integer') {
        $target_id_data_type = 'integer';
      }
    }

    if ($target_id_data_type === 'integer') {
      

  public function __construct(array $values$entity_type = 'base_field_override') {
    if (empty($values['field_name'])) {
      throw new FieldException('Attempt to create a base field bundle override of a field without a field_name');
    }
    if (empty($values['entity_type'])) {
      throw new FieldException("Attempt to create a base field bundle override of field {$values['field_name']} without an entity_type");
    }
    if (empty($values['bundle'])) {
      throw new FieldException("Attempt to create a base field bundle override of field {$values['field_name']} without a bundle");
    }

    parent::__construct($values$entity_type);
  }

  

  public function __construct(array $values$entity_type = 'field_storage_config') {
    // Check required properties.     if (empty($values['field_name'])) {
      throw new FieldException('Attempt to create a field storage without a field name.');
    }
    if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $values['field_name'])) {
      throw new FieldException("Attempt to create a field storage {$values['field_name']} with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character");
    }
    if (empty($values['type'])) {
      throw new FieldException("Attempt to create a field storage {$values['field_name']} with no type.");
    }
    if (empty($values['entity_type'])) {
      throw new FieldException("Attempt to create a field storage {$values['field_name']} with no entity_type.");
    }

    

  public function setInitialValue($value) {
    // @todo Implement initial value support for multi-value fields in     // https://www.drupal.org/node/2883851.     if ($this->isMultiple()) {
      throw new FieldException('Multi-value fields can not have an initial value.');
    }

    $this->definition['initial_value'] = $this->normalizeValue($value$this->getMainPropertyName());
    return $this;
  }

  /** * Returns the name of the field that will be used for getting initial values. * * @return string|null * The field name. */

  public function __construct(array $values$entity_type = 'field_config') {
    // Allow either an injected FieldStorageConfig object, or a field_name and     // entity_type.     if (isset($values['field_storage'])) {
      if (!$values['field_storage'] instanceof FieldStorageConfigInterface) {
        throw new FieldException('Attempt to create a configurable field for a non-configurable field storage.');
      }
      $field_storage = $values['field_storage'];
      $values['field_name'] = $field_storage->getName();
      $values['entity_type'] = $field_storage->getTargetEntityTypeId();
      // The internal property is fieldStorage, not field_storage.       unset($values['field_storage']);
      $values['fieldStorage'] = $field_storage;
    }
    else {
      if (empty($values['field_name'])) {
        throw new FieldException('Attempt to create a field without a field_name.');
      }
$schema[$tables['revision_data_table']] = $this->initializeRevisionDataTable($entity_type);
      }

      // We need to act only on shared entity schema tables.       $table_names = array_diff($table_mapping->getTableNames()$table_mapping->getDedicatedTableNames());
      foreach ($table_names as $table_name) {
        if (!isset($schema[$table_name])) {
          $schema[$table_name] = [];
        }
        foreach ($table_mapping->getFieldNames($table_name) as $field_name) {
          if (!isset($this->fieldStorageDefinitions[$field_name])) {
            throw new FieldException("Field storage definition for '$field_name' could not be found.");
          }
          // Add the schema for base field definitions.           elseif ($table_mapping->allowsSharedTableStorage($this->fieldStorageDefinitions[$field_name])) {
            $column_names = $table_mapping->getColumnNames($field_name);
            $storage_definition = $this->fieldStorageDefinitions[$field_name];
            $schema[$table_name] = array_merge_recursive($schema[$table_name]$this->getSharedTableFieldSchema($storage_definition$table_name$column_names));
          }
        }
      }

      // Process tables after having gathered field information.
Home | Imprint | This part of the site doesn't use cookies.