collectRenderDisplay example


  protected function init(FormStateInterface $form_state) {
    // Ensure we act on the translation object corresponding to the current form     // language.     $this->initFormLangcodes($form_state);
    $langcode = $this->getFormLangcode($form_state);
    $this->entity = $this->entity->hasTranslation($langcode) ? $this->entity->getTranslation($langcode) : $this->entity->addTranslation($langcode);

    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation());
    $this->setFormDisplay($form_display$form_state);

    parent::init($form_state);
  }

  /** * Initializes form language code values. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
  

  protected function getSingleFieldDisplay($entity$field_name$display_options) {
    if (is_string($display_options)) {
      // View mode: use the Display configured for the view mode.       $view_mode = $display_options;
      $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
      // Hide all fields except the current one.       foreach (array_keys($entity->getFieldDefinitions()) as $name) {
        if ($name != $field_name) {
          $display->removeComponent($name);
        }
      }
    }
    else {
      // Array of custom display options: use a runtime Display for the       // '_custom' view mode. Persist the displays created, to reduce the number       // of objects (displays and formatter plugins) created when rendering a
$class = new TestLayoutEntityHelperTrait();
    $result = $class->getSectionStorageForEntity($entity);
    $this->assertEquals($expected_context_keysarray_keys($result));
    if ($entity instanceof EntityViewDisplayInterface) {
      $this->assertEquals(EntityContext::fromEntity($entity)$result['display']);
    }
    elseif ($entity instanceof FieldableEntityInterface) {
      $this->assertEquals(EntityContext::fromEntity($entity)$result['entity']);
      $this->assertInstanceOf(Context::class$result['view_mode']);
      $this->assertEquals('full', $result['view_mode']->getContextData()->getValue());

      $expected_display = EntityViewDisplay::collectRenderDisplay($entity, 'full');
      $this->assertInstanceOf(EntityContext::class$result['display']);
      /** @var \Drupal\Core\Plugin\Context\EntityContext $display_entity_context */
      $display_entity_context = $result['display'];

      /** @var \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay $display_entity */
      $display_entity = $display_entity_context->getContextData()->getValue();
      $this->assertInstanceOf(LayoutBuilderEntityViewDisplay::class$display_entity);

      $this->assertEquals('full', $display_entity->getMode());
      $this->assertEquals($expected_display->getEntityTypeId()$display_entity->getEntityTypeId());
      $this->assertEquals($expected_display->getComponents()$display_entity->getComponents());
      

  protected function getDisplay($entity_type_id) {
    if ($entity = $this->routeMatch->getParameter($entity_type_id)) {
      if ($entity instanceof FieldableEntityInterface) {
        // @todo Expand to work for all view modes in         // https://www.drupal.org/node/2907413.         $view_mode = 'full';
        $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
        if ($display instanceof LayoutEntityDisplayInterface) {
          return $display;
        }
      }
    }
  }

}

  public static function processBlockForm(array $element, FormStateInterface $form_state) {
    /** @var \Drupal\block_content\BlockContentInterface $block */
    $block = $element['#block'];
    EntityFormDisplay::collectRenderDisplay($block, 'edit')->buildForm($block$element$form_state);
    $element['revision_log']['#access'] = FALSE;
    $element['info']['#access'] = FALSE;
    return $element;
  }

  /** * {@inheritdoc} */
  public function blockValidate($form, FormStateInterface $form_state) {
    $block_form = $form['block_form'];
    /** @var \Drupal\block_content\BlockContentInterface $block */
    


  /** * {@inheritdoc} */
  public function elementValidate($element, FormStateInterface $form_state$form) {
    $field_name = $element['#field_name'];
    $entity = $form_state->getFormObject()->getEntity();
    $input = $form_state->getUserInput();
    if (!empty($input['_triggering_element_name']) && str_contains($input['_triggering_element_name'], 'media-library-update')) {
      // This will validate a required field before an upload is completed.       $display = EntityFormDisplay::collectRenderDisplay($entity, 'edit');
      $display->extractFormValues($entity$form$form_state);
      $display->validateFormValues($entity$form$form_state);
    }
    $form_value = $form_state->getValue($field_name);
    if (!empty($form_value['media_library_selection'])) {
      $entity->set($field_name$form_value['media_library_selection']);
    }
  }

}
protected function getSectionStorageForEntity(EntityInterface $entity) {
    // @todo Take into account other view modes in     // https://www.drupal.org/node/3008924.     $view_mode = 'full';
    if ($entity instanceof LayoutEntityDisplayInterface) {
      $contexts['display'] = EntityContext::fromEntity($entity);
      $contexts['view_mode'] = new Context(new ContextDefinition('string')$entity->getMode());
    }
    else {
      $contexts['entity'] = EntityContext::fromEntity($entity);
      if ($entity instanceof FieldableEntityInterface) {
        $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
        if ($display instanceof LayoutEntityDisplayInterface) {
          $contexts['display'] = EntityContext::fromEntity($display);
        }
        $contexts['view_mode'] = new Context(new ContextDefinition('string')$view_mode);
      }
    }
    return $this->sectionStorageManager()->findByContext($contextsnew CacheableMetadata());
  }

  /** * Determines if the original entity used the default section storage. * * This method can be used during the entity save process to determine whether * $entity->original is set and used the default section storage plugin as * determined by ::getSectionStorageForEntity(). * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * * @return bool * TRUE if the original entity used the default storage. */
    // https://www.drupal.org/node/2988223     $source = $media->getSource();
    $plugin_definition = $source->getPluginDefinition();
    if ($thumbnail_uri = $source->getMetadata($media$plugin_definition['thumbnail_uri_metadata_attribute'])) {
      $element['preview']['thumbnail'] = [
        '#theme' => 'image_style',
        '#style_name' => 'media_library',
        '#uri' => $thumbnail_uri,
      ];
    }

    $form_display = EntityFormDisplay::collectRenderDisplay($media, 'media_library');
    // When the name is not added to the form as an editable field, output     // the name as a fixed element to confirm the right file was uploaded.     if (!$form_display->getComponent('name')) {
      $element['fields']['name'] = [
        '#type' => 'item',
        '#title' => $this->t('Name'),
        '#markup' => $media->getName(),
      ];
    }
    $form_display->buildForm($media$element['fields']$form_state);

    

  public function getBaseFormId() {
    return $this->getEntity()->getEntityTypeId() . '_layout_builder_form';
  }

  /** * {@inheritdoc} */
  protected function init(FormStateInterface $form_state) {
    parent::init($form_state);

    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation(), FALSE);
    $form_display->setComponent(OverridesSectionStorage::FIELD_NAME, [
      'type' => 'layout_builder_widget',
      'weight' => -10,
      'settings' => [],
    ]);

    $this->setFormDisplay($form_display$form_state);
  }

  /** * {@inheritdoc} */

  protected function renderTestEntity($id$view_mode = 'full', $reset = TRUE) {
    if ($reset) {
      $this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
    }
    $entity = EntityTest::load($id);
    $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
    $build = $display->build($entity);
    return (string) $this->container->get('renderer')->renderRoot($build);
  }

  /** * Sets the site timezone to a given timezone. * * @param string $timezone * The timezone identifier to set. */
  protected function setSiteTimezone($timezone) {
    

  public function deriveContextsFromRoute($value$definition$name, array $defaults) {
    $contexts = [];

    if ($entity = $this->extractEntityFromRoute($value$defaults)) {
      $contexts['entity'] = EntityContext::fromEntity($entity);
      // @todo Expand to work for all view modes in       // https://www.drupal.org/node/2907413.       $view_mode = 'full';
      // Retrieve the actual view mode from the returned view display as the       // requested view mode may not exist and a fallback will be used.       $view_mode = LayoutBuilderEntityViewDisplay::collectRenderDisplay($entity$view_mode)->getMode();
      $contexts['view_mode'] = new Context(new ContextDefinition('string')$view_mode);
    }
    return $contexts;
  }

  /** * Extracts an entity from the route values. * * @param mixed $value * The raw value from the route. * @param array $defaults * The route defaults array. * * @return \Drupal\Core\Entity\EntityInterface|null * The entity for the route, or NULL if none exist. * * @see \Drupal\layout_builder\SectionStorageInterface::deriveContextsFromRoute() * @see \Drupal\Core\ParamConverter\ParamConverterInterface::convert() */

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['foo'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Wrapper'),
      '#tree' => TRUE,
      '#parents' => ['foo'],
    ];
    $entity = EntityTest::load(1);
    if ($entity) {
      if ($entity) {
        $display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
        $subform_state = SubformState::createForSubform($form['foo']$form$form_state);
        $display->buildForm($entity$form['foo']$subform_state);
        \Drupal::classResolver(FieldLayoutBuilder::class)->buildForm($form['foo']$display$subform_state);
      }
    }
    return $form;
  }

  /** * {@inheritdoc} */
  
public function testSeparatorTranslation() {
    // Create an entity.     $entity = EntityTest::create([
      'name' => $this->randomString(),
      $this->fieldStorage->getName() => [
        'value' => '2016-09-20',
        'end_value' => '2016-09-21',
      ],
    ]);

    // Verify the untranslated separator.     $display = EntityViewDisplay::collectRenderDisplay($entity, 'default');
    $build = $display->build($entity);
    $output = $this->container->get('renderer')->renderRoot($build);
    $this->assertStringContainsString('UNTRANSLATED', (string) $output);

    // Translate the separator.     ConfigurableLanguage::createFromLangcode('nl')->save();
    /** @var \Drupal\language\ConfigurableLanguageManagerInterface $language_manager */
    $language_manager = $this->container->get('language_manager');
    $language_manager->getLanguageConfigOverride('nl', 'core.entity_view_display.entity_test.entity_test.default')
      ->set('content.' . $this->fieldStorage->getName() . '.settings.separator', 'NL_TRANSLATED!')
      ->save();

    

  public function getFormId() {
    return 'field_test_entity_nested_form';
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) {
    // First entity.     $form_state->set('entity_1', $entity_1);
    $form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default');
    $form_state->set('form_display_1', $form_display_1);
    $form_display_1->buildForm($entity_1$form$form_state);

    // Second entity.     $form_state->set('entity_2', $entity_2);
    $form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default');
    $form_state->set('form_display_2', $form_display_2);
    $form['entity_2'] = [
      '#type' => 'details',
      '#title' => t('Second entity'),
      '#tree' => TRUE,
      
Home | Imprint | This part of the site doesn't use cookies.