You are here

public static function RateWidget::postDelete in Rate 8.2

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

Overrides EntityBase::postDelete

File

src/Entity/RateWidget.php, line 167

Class

RateWidget
Defines the Rate Widget configuration entity.

Namespace

Drupal\rate\Entity

Code

public static function postDelete(EntityStorageInterface $storage, array $entities) {
  parent::postDelete($storage, $entities);

  // Check if the widget used a deadline field and delete it.
  foreach ($entities as $rate_widget) {
    $use_deadline = $rate_widget
      ->get('voting')['use_deadline'];
    $current_entities = $rate_widget
      ->get('entity_types');
    $field_name = 'field_rate_vote_deadline';
    if ($use_deadline == 1 && !empty($current_entities)) {
      foreach ($current_entities as $entity) {
        $parameter = explode('.', $entity);
        $field_config = FieldConfig::loadByName($parameter[0], $parameter[1], $field_name);
        if (!empty($field_config)) {
          $field_config
            ->delete();
        }
      }
    }
  }

  // Clear the rate widget cache to reflect the removal.
  $storage
    ->resetCache(array_keys($entities));

  // Clear cached field definitions to remove rate widget extra field.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
}