You are here

function entity_usage_form_alter in Entity Usage 8.4

Same name and namespace in other branches
  1. 8.2 entity_usage.module \entity_usage_form_alter()
  2. 8.3 entity_usage.module \entity_usage_form_alter()

Implements hook_form_alter().

File

./entity_usage.module, line 42
Contains entity_usage.module.

Code

function entity_usage_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state
    ->getFormObject();
  if (!method_exists($form_object, 'getEntity')) {
    return;
  }

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $form_object
    ->getEntity();
  if (empty($entity)) {
    return;
  }
  $config = \Drupal::config('entity_usage.settings');
  $edit_entity_types = $config
    ->get('edit_warning_message_entity_types') ?: [];
  $delete_entity_types = $config
    ->get('delete_warning_message_entity_types') ?: [];

  // Abort early if this entity is not configured to show any message.
  if (!in_array($entity
    ->getEntityTypeId(), $edit_entity_types) && !in_array($entity
    ->getEntityTypeId(), $delete_entity_types)) {
    return;
  }
  $usage_data = \Drupal::service('entity_usage.usage')
    ->listSources($entity);
  if (empty($usage_data)) {
    return;
  }

  // Check for the edit warning.
  if (method_exists($form_object, 'getOperation') && $form_object
    ->getOperation() === 'edit' && in_array($entity
    ->getEntityTypeId(), $edit_entity_types)) {
    $form['entity_usage_edit_warning'] = [
      '#theme' => 'status_messages',
      '#message_list' => [
        'warning' => [
          t('Modifications on this form will affect all <a href="@usage_url" target="_blank">existing usages</a> of this entity.', [
            '@usage_url' => Url::fromRoute('entity_usage.usage_list', [
              'entity_type' => $entity
                ->getEntityTypeId(),
              'entity_id' => $entity
                ->id(),
            ])
              ->toString(),
          ]),
        ],
      ],
      '#status_headings' => [
        'warning' => t('Warning message'),
      ],
      '#weight' => -201,
    ];
  }
  elseif (in_array($entity
    ->getEntityTypeId(), $delete_entity_types)) {

    // Even if this is not on the UI, sites can define additional form classes
    // where the delete message can be shown.
    $form_classes = $config
      ->get('delete_warning_form_classes') ?: [
      'Drupal\\Core\\Entity\\ContentEntityDeleteForm',
    ];
    $is_delete_form = FALSE;
    foreach ($form_classes as $class) {
      if ($form_object instanceof $class) {
        $is_delete_form = TRUE;
        break;
      }
    }
    if ($is_delete_form) {
      $form['entity_usage_delete_warning'] = [
        '#theme' => 'status_messages',
        '#message_list' => [
          'warning' => [
            t('There are <a href="@usage_url" target="_blank">recorded usages</a> of this entity.', [
              '@usage_url' => Url::fromRoute('entity_usage.usage_list', [
                'entity_type' => $entity
                  ->getEntityTypeId(),
                'entity_id' => $entity
                  ->id(),
              ])
                ->toString(),
            ]),
          ],
        ],
        '#status_headings' => [
          'warning' => t('Warning message'),
        ],
        '#weight' => -201,
      ];
    }
  }
}