You are here

function event_add_description_field in Event 8

Adds the default description field to an event type.

Parameters

\Drupal\event\Entity\EventTypeInterface $type: An event type object.

string $label: (optional) The label for the description instance.

Return value

\Drupal\field\Entity\FieldConfig A description field object.

2 calls to event_add_description_field()
EventTypeForm::save in src/Form/EventTypeForm.php
Form submission handler for the 'save' action.
event_update_8102 in ./event.install
Install the Machine Name, Date Range, and Description Fields.

File

./event.module, line 76
Contains event.module.

Code

function event_add_description_field(EventTypeInterface $type, $label = 'Description') {

  // Add or remove the description field, as needed.
  $field_storage = FieldStorageConfig::loadByName('event', 'description');
  $field = FieldConfig::loadByName('event', $type
    ->id(), 'description');
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $type
        ->id(),
      'label' => $label,
      'settings' => [
        'display_summary' => TRUE,
      ],
    ]);
    $field
      ->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    // Assign widget settings for the default form mode.
    $display_repository
      ->getFormDisplay('event', $type
      ->id())
      ->setComponent('description', [
      'type' => 'text_textarea_with_summary',
    ])
      ->save();

    // Assign display settings for the 'default' and 'teaser' view modes.
    $display_repository
      ->getViewDisplay('event', $type
      ->id())
      ->setComponent('description', [
      'label' => 'hidden',
      'type' => 'text_default',
    ])
      ->save();

    // The teaser view mode is created by the Standard profile and therefore
    // might not exist.
    $view_modes = \Drupal::service('entity_display.repository')
      ->getViewModes('event');
    if (isset($view_modes['teaser'])) {
      $display_repository
        ->getViewDisplay('event', $type
        ->id(), 'teaser')
        ->setComponent('description', [
        'label' => 'hidden',
        'type' => 'text_summary_or_trimmed',
      ])
        ->save();
    }
  }
  return $field;
}