function smart_date_recur_entity_delete in Smart Date 3.0.x
Same name and namespace in other branches
- 8.2 modules/smart_date_recur/smart_date_recur.module \smart_date_recur_entity_delete()
- 3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_entity_delete()
- 3.1.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_entity_delete()
- 3.2.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_entity_delete()
- 3.3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_entity_delete()
- 3.4.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_entity_delete()
Implements hook_entity_delete().
File
- modules/
smart_date_recur/ smart_date_recur.module, line 66 - Field hooks for a field that stores a start and end date as timestamps.
Code
function smart_date_recur_entity_delete($entity) {
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
// Check for rules that apply to this entity type and bundle.
$query = \Drupal::entityTypeManager()
->getStorage('smart_date_rule');
$query_result = $query
->getQuery()
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute();
// If none exist, nothing to do.
if (!$query_result) {
return;
}
$field_names = [];
// Get all the relevant fields for this bundle.
foreach ($query_result as $rule) {
$rrule = SmartDateRule::load($rule);
if ($rrule) {
$field_name = $rrule->field_name->value;
if (!in_array($field_name, $field_names)) {
$field_names[] = $field_name;
}
}
}
// If no relevant fields for this bundle, nothing to do.
if (!$field_names) {
return;
}
// Check for relevant field values on the deleted entity.
foreach ($field_names as $field_name) {
if (!($values = $entity->{$field_name})) {
continue;
}
$rules = [];
// Collect all distinct rules defined for the deleted entity.
foreach ($values as $value) {
if ($value->rrule && !in_array($value->rrule, $rules)) {
$rules[] = $value->rrule;
}
}
// If no rules defined for this field, nothing to do.
if (!$rules) {
continue;
}
// Delete any rules found.
foreach ($rules as $rule) {
$rrule = SmartDateRule::load($rule);
$rrule
->delete();
}
}
}