You are here

function title_field_for_manage_display_entity_bundle_info_alter in Title Field for Manage Display 8

Same name and namespace in other branches
  1. 8.2 title_field_for_manage_display.module \title_field_for_manage_display_entity_bundle_info_alter()

Implements hook_entity_bundle_info_alter().

File

./title_field_for_manage_display.module, line 33
Contains title_field_for_manage_display.module.

Code

function title_field_for_manage_display_entity_bundle_info_alter(&$bundles) {
  if (\Drupal::service('config.installer')
    ->isSyncing()) {
    return;
  }
  $title_replacement_field = 'field_display_title';
  $title_replacement_field_label = 'Title';

  // Field storage.
  $field_storage_config = FieldStorageConfig::loadByName('node', $title_replacement_field);
  if (empty($field_storage_config)) {
    \Drupal\field\Entity\FieldStorageConfig::create(array(
      'field_name' => $title_replacement_field,
      'entity_type' => 'node',
      'type' => 'string',
      'description' => 'Manage Display Title',
      'translatable' => TRUE,
    ))
      ->save();
  }
  foreach (NodeType::loadMultiple() as $bundle) {
    $bundle_id = $bundle
      ->Id();

    // Bundle fields.
    if ($bundle_id != 'webform') {
      $field_config = FieldConfig::loadByName('node', $bundle_id, $title_replacement_field);
      if (empty($field_config)) {
        \Drupal\field\Entity\FieldConfig::create([
          'field_name' => $title_replacement_field,
          'entity_type' => 'node',
          'bundle' => $bundle_id,
          'label' => $title_replacement_field_label,
          'field_storage' => $field_storage_config,
        ])
          ->save();
        \Drupal::service('entity_display.repository')
          ->getFormDisplay('node', $bundle_id, 'default')
          ->removeComponent($title_replacement_field)
          ->save();
        \Drupal::service('entity_display.repository')
          ->getViewDisplay('node', $bundle_id, 'default')
          ->removeComponent($title_replacement_field)
          ->save();
      }
    }
  }
}