You are here

public function ProductAttributeFieldManager::canDeleteField in Commerce Core 8.2

Checks whether the attribute field for the given attribute can be deleted.

An attribute field is no longer deletable once it has data.

Parameters

\Drupal\commerce_product\Entity\ProductAttributeInterface $attribute: The product attribute.

string $variation_type_id: The product variation type ID.

Return value

bool TRUE if the attribute field can be deleted, FALSE otherwise.

Throws

\InvalidArgumentException Thrown when the attribute field does not exist.

Overrides ProductAttributeFieldManagerInterface::canDeleteField

1 call to ProductAttributeFieldManager::canDeleteField()
ProductAttributeFieldManager::deleteField in modules/product/src/ProductAttributeFieldManager.php
Deletes the attribute field for the given attribute.

File

modules/product/src/ProductAttributeFieldManager.php, line 216

Class

ProductAttributeFieldManager
Default implementation of the ProductAttributeFieldManagerInterface.

Namespace

Drupal\commerce_product

Code

public function canDeleteField(ProductAttributeInterface $attribute, $variation_type_id) {
  $field_name = $this
    ->buildFieldName($attribute);
  $field = FieldConfig::loadByName('commerce_product_variation', $variation_type_id, $field_name);
  if (!$field) {

    // The matching field was already deleted, or follows a different naming
    // pattern, because it wasn't created by this class.
    return FALSE;
  }
  $query = $this->entityTypeManager
    ->getStorage('commerce_product_variation')
    ->getQuery()
    ->condition('type', $variation_type_id)
    ->accessCheck(FALSE)
    ->exists($field_name)
    ->range(0, 1);
  $result = $query
    ->execute();
  return empty($result);
}