You are here

public function GdprField::propertyCanBeRemoved in General Data Protection Regulation 8.2

Same name and namespace in other branches
  1. 8 modules/gdpr_fields/src/Entity/GdprField.php \Drupal\gdpr_fields\Entity\GdprField::propertyCanBeRemoved()
  2. 3.0.x modules/gdpr_fields/src/Entity/GdprField.php \Drupal\gdpr_fields\Entity\GdprField::propertyCanBeRemoved()

Check whether a property can be removed.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The property info.

string $error_message: A variable to fill with an error message.

Return value

bool TRUE if the property can be removed, FALSE if not.

File

modules/gdpr_fields/src/Entity/GdprField.php, line 369

Class

GdprField
Metadata for a GDPR field.

Namespace

Drupal\gdpr_fields\Entity

Code

public function propertyCanBeRemoved(FieldDefinitionInterface $field_definition, &$error_message = NULL) {
  if ($field_definition
    ->isComputed()) {
    $error_message = new TranslatableMarkup('Unable to remove computed field %field.', [
      '%field' => $field_definition
        ->getName(),
    ]);
    return FALSE;
  }
  if ($field_definition
    ->isRequired()) {
    $error_message = new TranslatableMarkup('Unable to remove required field %field.', [
      '%field' => $field_definition
        ->getName(),
    ]);
    return FALSE;
  }
  if ($field_definition
    ->isReadOnly()) {
    $error_message = new TranslatableMarkup('Unable to remove readonly field %field.', [
      '%field' => $field_definition
        ->getName(),
    ]);
    return FALSE;
  }

  // @todo Find something less generic than `EntityTypeInterface::getKeys()`.
  $entity_keys = [
    'id',
    'uuid',
    'revision',
    'bundle',
    'label',
    'langcode',
    'default_langcode',
    'revision_translation_affected',
  ];
  if (in_array($field_definition
    ->getName(), $entity_keys)) {
    $error_message = new TranslatableMarkup('Unable to remove entity key %field.', [
      '%field' => $field_definition
        ->getName(),
    ]);
    return FALSE;
  }
  return TRUE;
}