prepareViewDisplay example


class TestDifferentDisplays extends Test {

  /** * {@inheritdoc} */
  public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
    parent::prepareViewDisplay($type$display);
    $source_name = $this->getSourceFieldDefinition($type)->getName();
    $source_component = $display->getComponent($source_name) ?: [];
    $source_component['type'] = 'entity_reference_entity_id';
    $display->setComponent($source_name$source_component);
  }

  /** * {@inheritdoc} */
  public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
    parent::prepareFormDisplay($type$display);
    
// Reset the field to its default settings so that we don't inherit the     // settings from the parent class' source field.     $settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType());

    return $field->set('settings', $settings);
  }

  /** * {@inheritdoc} */
  public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
    parent::prepareViewDisplay($type$display);

    // Use the `large` image style and do not link the image to anything.     // This will prevent the out-of-the-box configuration from outputting very     // large raw images. If the `large` image style has been deleted, do not     // set an image style.     $field_name = $this->getSourceFieldDefinition($type)->getName();
    $component = $display->getComponent($field_name);
    $component['settings']['image_link'] = '';
    $component['settings']['image_style'] = '';
    if ($this->entityTypeManager->getStorage('image_style')->load('large')) {
      $component['settings']['image_style'] = 'large';
    }
$display = $this->entityDisplayRepository->getFormDisplay('media', $media_type->id());
        $source->prepareFormDisplay($media_type$display);
        $display->save();
      }
      if ($source_field->isDisplayConfigurable('view')) {
        $display = $this->entityDisplayRepository->getViewDisplay('media', $media_type->id());

        // Remove all default components.         foreach (array_keys($display->getComponents()) as $name) {
          $display->removeComponent($name);
        }
        $source->prepareViewDisplay($media_type$display);
        $display->save();
      }
    }

    $t_args = ['%name' => $media_type->label()];
    if ($status === SAVED_UPDATED) {
      $this->messenger()->addStatus($this->t('The media type %name has been updated.', $t_args));
    }
    elseif ($status === SAVED_NEW) {
      $this->messenger()->addStatus($this->t('The media type %name has been added.', $t_args));
      $this->logger('media')->notice('Added media type %name.', $t_args);
    }
Home | Imprint | This part of the site doesn't use cookies.