getViewModes example

// Entity view displays: assign widget settings for the default view       // mode, and hide the field in all other view modes.       $entity_display_repository->getViewDisplay($entity_type$bundle)
        ->setComponent($field_name[
          'label' => 'above',
          'type' => 'comment_default',
          'weight' => 20,
          'settings' => ['view_mode' => $comment_view_mode],
        ])
        ->save();
      foreach ($entity_display_repository->getViewModes($entity_type) as $id => $view_mode) {
        $display = $entity_display_repository->getViewDisplay($entity_type$bundle$id);
        // Only update existing displays.         if ($display && !$display->isNew()) {
          $display->removeComponent($field_name)->save();
        }
      }
    }
  }

}
/** * {@inheritdoc} */
  protected function getDefaultPlugin($field_type) {
    return $this->fieldTypes[$field_type]['default_formatter'] ?? NULL;
  }

  /** * {@inheritdoc} */
  protected function getDisplayModes() {
    return $this->entityDisplayRepository->getViewModes($this->entity->getTargetEntityTypeId());
  }

  /** * {@inheritdoc} */
  protected function getDisplayModeOptions() {
    return $this->entityDisplayRepository->getViewModeOptions($this->entity->getTargetEntityTypeId());
  }

  /** * {@inheritdoc} */

  protected function isViewModeCacheable($view_mode) {
    if ($view_mode == 'default') {
      // The 'default' is not an actual view mode.       return TRUE;
    }
    $view_modes_info = $this->entityDisplayRepository->getViewModes($this->entityTypeId);
    return !empty($view_modes_info[$view_mode]['cache']);
  }

  /** * {@inheritdoc} */
  public function viewField(FieldItemListInterface $items$display_options = []) {
    /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
    $entity = $items->getEntity();
    // If the field is not translatable and the entity is, then the field item     // list always points to the default translation of the entity. Attempt to

    }

    return $elements;
  }

  /** * {@inheritdoc} */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = [];
    $view_modes = $this->getViewModes();
    $element['view_mode'] = [
      '#type' => 'select',
      '#title' => $this->t('Comments view mode'),
      '#description' => $this->t('Select the view mode used to show the list of comments.'),
      '#default_value' => $this->getSetting('view_mode'),
      '#options' => $view_modes,
      // Only show the select element when there are more than one options.       '#access' => count($view_modes) > 1,
    ];
    $element['pager_id'] = [
      '#type' => 'select',
      
'route_name' => "entity.entity_form_display.$entity_type_id.form_mode",
            'route_parameters' => [
              'form_mode_name' => $form_mode,
            ],
            'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id",
            'weight' => array_flip($form_modes_titles)[(string) $form_mode_info['label']],
            'cache_tags' => $this->entityTypeManager->getDefinition('entity_form_display')->getListCacheTags(),
          ];
        }

        // One local task for each view mode.         $view_modes = $this->entityDisplayRepository->getViewModes($entity_type_id);
        // Sort all view modes by title.         $view_modes_titles = array_values(array_map(fn($item) => (string) $item['label']$view_modes));
        sort($view_modes_titles, SORT_NATURAL);
        foreach ($view_modes as $view_mode => $form_mode_info) {
          $this->derivatives['field_display_' . $view_mode . '_' . $entity_type_id] = [
            'title' => $form_mode_info['label'],
            'route_name' => "entity.entity_view_display.$entity_type_id.view_mode",
            'route_parameters' => [
              'view_mode_name' => $view_mode,
            ],
            'parent_id' => "field_ui.fields:display_overview_$entity_type_id",
            

  protected function initializeIterator() {
    $rows = [];
    $result = $this->prepareQuery()->execute();
    while ($field_row = $result->fetchAssoc()) {
      // These are added to every view mode row.       $field_row['display_settings'] = unserialize($field_row['display_settings']);
      $field_row['widget_settings'] = unserialize($field_row['widget_settings']);
      $bundle = $field_row['type_name'];
      $field_name = $field_row['field_name'];

      foreach ($this->getViewModes() as $view_mode) {
        // Append to the return value if the row has display settings for this         // view mode and the view mode is neither hidden nor excluded.         // @see \Drupal\node\Plugin\migrate\source\d6\ViewMode::initializeIterator()         if (isset($field_row['display_settings'][$view_mode]) && $field_row['display_settings'][$view_mode]['format'] != 'hidden' && empty($field_row['display_settings'][$view_mode]['exclude'])) {
          $index = $view_mode . "." . $bundle . "." . $field_name;
          $rows[$index]['entity_type'] = 'node';
          $rows[$index]['view_mode'] = $view_mode;
          $rows[$index]['type_name'] = $bundle;
          $rows[$index]['field_name'] = $field_name;
          $rows[$index]['type'] = $field_row['type'];
          $rows[$index]['module'] = $field_row['module'];
          

class ViewMode extends ViewModeBase {

  /** * {@inheritdoc} */
  protected function initializeIterator() {
    $rows = [];
    $result = $this->prepareQuery()->execute();
    while ($field_row = $result->fetchAssoc()) {
      $field_row['display_settings'] = unserialize($field_row['display_settings']);
      foreach ($this->getViewModes() as $view_mode) {
        // Append to the return value if the row has display settings for this         // view mode and the view mode is neither hidden nor excluded.         // @see \Drupal\field\Plugin\migrate\source\d6\FieldInstancePerViewMode::initializeIterator()         if (isset($field_row['display_settings'][$view_mode]) && $field_row['display_settings'][$view_mode]['format'] != 'hidden' && empty($field_row['display_settings'][$view_mode]['exclude'])) {
          if (!isset($rows[$view_mode])) {
            $rows[$view_mode]['entity_type'] = 'node';
            $rows[$view_mode]['view_mode'] = $view_mode;
          }
        }
      }
    }

    
'#type' => 'select',
      '#title' => $this->t('Display type'),
      '#options' => $this->buildOptionsForm_summary_options(),
      '#default_value' => $this->options['view_mode'],
    ];
  }

  /** * Return the main options, which are shown in the summary title. */
  public function buildOptionsForm_summary_options() {
    $view_modes = $this->entityDisplayRepository->getViewModes($this->entityTypeId);
    $options = [];
    foreach ($view_modes as $mode => $settings) {
      $options[$mode] = $settings['label'];
    }
    return $options;
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    
Home | Imprint | This part of the site doesn't use cookies.