You are here

private function Update400::getQuestionForMediaReferenceField in Lightning Media 8.4

Generates a question before migrating a field in an entity form display.

Parameters

\Drupal\Core\Entity\Display\EntityFormDisplayInterface $display: The entity form display being processed.

string $name: The component name.

Return value

string The question to ask.

1 call to Update400::getQuestionForMediaReferenceField()
Update400::convertMediaReferenceFieldsToMediaLibrary in src/Update/Update400.php
Optionally changes entity browser field widgets to media library widgets.

File

src/Update/Update400.php, line 193

Class

Update400
Contains optional updates targeting Lightning Media 4.0.0.

Namespace

Drupal\lightning_media\Update

Code

private function getQuestionForMediaReferenceField(EntityFormDisplayInterface $display, $name) {
  $entity_type = $display
    ->getTargetEntityTypeId();
  $bundle = $display
    ->getTargetBundle();
  $variables = [];

  // Get the human-readable field label (e.g. 'Images').
  $variables['@field'] = $this->entityTypeManager
    ->getStorage('field_config')
    ->load("{$entity_type}.{$bundle}.{$name}")
    ->getLabel();

  // Get the human-readable name of the form mode (e.g., 'Media Browser').
  $form_mode = $display
    ->getMode();

  // If this display is for the default form mode, it will not be possible to
  // load the form mode as an entity, so just hard-code the name of the form
  // mode to 'default'.
  if ($form_mode === EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE) {
    $variables['@form_mode'] = (string) $this
      ->t('default');
  }
  else {
    $variables['@form_mode'] = $this->entityTypeManager
      ->getStorage('entity_form_mode')
      ->load("{$entity_type}.{$form_mode}")
      ->label();
  }
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type);
  $bundle_entity_type = $entity_type
    ->getBundleEntityType();
  if ($bundle_entity_type) {

    // The human-readable name of the bundle entity type, e.g. 'content type'.
    $variables['@bundle_type'] = lcfirst($entity_type
      ->getBundleLabel());

    // The actual label of the specific bundle in use, e.g. 'Article'.
    $variables['@bundle'] = $this->entityTypeManager
      ->getStorage($bundle_entity_type)
      ->load($bundle)
      ->label();
    $question = $this
      ->t('Do you want to convert the @field field of the @bundle @bundle_type to use the media library in the @form_mode form mode?', $variables);
  }
  else {

    // The plural human-readable name of the of the entity type targeted by
    // the display, e.g., 'content items'.
    $variables['@entity_type'] = $this->entityTypeManager
      ->getDefinition($entity_type)
      ->getPluralLabel();
    $question = $this
      ->t('Do you want to convert the @field field of @entity_type to use the media library in the @form_mode form mode?', $variables);
  }
  return (string) $question;
}