You are here

function message_notify_entity_bundle_create in Message Notify 8

Implements hook_entity_bundle_create().

We cannot easily set the the visibilty of extra fields, so we set the bundle settings upon creation of new message bundle.

File

./message_notify.module, line 26
Message notify module.

Code

function message_notify_entity_bundle_create($entity_type_id, $bundle) {
  if ($entity_type_id != 'message') {
    return;
  }

  // Do nothing if these view modes are not yet available.
  if (!EntityViewMode::load('message.mail_body') || !EntityViewMode::load('message.mail_subject')) {
    return;
  }

  // If this bundle is being created from a .yml file, ignore.
  if (\Drupal::isConfigSyncing()) {
    return;
  }
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display');

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $default */
  if (method_exists(\Drupal::service('entity_display.repository'), 'getViewDisplay')) {
    $default = \Drupal::service('entity_display.repository')
      ->getViewDisplay($entity_type_id, $bundle);
  }
  else {

    // Pre-Drupal 8.8.
    $default = entity_get_display($entity_type_id, $bundle, 'default');
  }

  // Setup the subject/title display mode if it doesn't exist.
  if (!$storage
    ->load($entity_type_id . '.' . $bundle . '.mail_subject')) {
    $display = $default
      ->createCopy('mail_subject');
    $display
      ->set('content', [
      'partial_0' => [
        'weight' => 0,
      ],
    ]);
    $display
      ->set('hidden', [
      'partial_1' => TRUE,
    ]);
    $display
      ->save();
  }

  // Setup the body display if it doesn't exist.
  if (!$storage
    ->load($entity_type_id . '.' . $bundle . '.mail_body')) {
    $display = $default
      ->createCopy('mail_body');
    $display
      ->set('content', [
      'partial_1' => [
        'weight' => 0,
      ],
    ]);
    $display
      ->set('hidden', [
      'partial_0' => TRUE,
    ]);
    $display
      ->save();
  }
}