entityFormBuilder example


  public function edit(ViewUI $view$display_id = NULL) {
    $name = $view->label();
    $data = $this->viewsData->get($view->get('base_table'));

    if (isset($data['table']['base']['title'])) {
      $name .= ' (' . $data['table']['base']['title'] . ')';
    }
    $build['#title'] = $name;

    $build['edit'] = $this->entityFormBuilder()->getForm($view, 'edit', ['display_id' => $display_id]);
    $build['preview'] = $this->entityFormBuilder()->getForm($view, 'preview', ['display_id' => $display_id]);
    return $build;
  }

}
/** * Returns add forum entity form. * * @return array * Render array for the add form. */
  public function addForum() {
    $vid = $this->config('forum.settings')->get('vocabulary');
    $taxonomy_term = $this->termStorage->create([
      'vid' => $vid,
    ]);
    return $this->entityFormBuilder()->getForm($taxonomy_term, 'forum');
  }

  /** * Returns add container entity form. * * @return array * Render array for the add form. */
  public function addContainer() {
    $vid = $this->config('forum.settings')->get('vocabulary');
    $taxonomy_term = $this->termStorage->create([
      
else {
      $build['#title'] = $this->t('Preview comment');
    }

    // Show the actual reply box.     $comment = $this->entityTypeManager()->getStorage('comment')->create([
      'entity_id' => $entity->id(),
      'pid' => $pid,
      'entity_type' => $entity->getEntityTypeId(),
      'field_name' => $field_name,
    ]);
    $build['comment_form'] = $this->entityFormBuilder()->getForm($comment);

    return $build;
  }

  /** * Access check for the reply form. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity this comment belongs to. * @param string $field_name * The field_name to which the comment belongs. * @param int $pid * (optional) Some comments are replies to other comments. In those cases, * $pid is the parent comment's comment ID. Defaults to NULL. * * @return \Drupal\Core\Access\AccessResultInterface * An access result * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */
    // See https://www.drupal.org/node/2006348.
    // Use the add form handler, if available, otherwise default.     $operation = $entity->getEntityType()->hasHandlerClass('form', 'add') ? 'add' : 'default';

    $form_state_additions = [];
    $form_state_additions['langcode'] = $target->getId();
    $form_state_additions['content_translation']['source'] = $source;
    $form_state_additions['content_translation']['target'] = $target;
    $form_state_additions['content_translation']['translation_form'] = !$entity->access('update');

    return $this->entityFormBuilder()->getForm($entity$operation$form_state_additions);
  }

  /** * Builds the edit translation page. * * @param \Drupal\Core\Language\LanguageInterface $language * The language of the translated values. Defaults to the current content * language. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match object from which to extract the entity type. * @param string $entity_type_id * (optional) The entity type ID. * * @return array * A processed form array ready to be rendered. */

  public function twoFormInstances() {
    $user = $this->currentUser();
    $values = [
      'uid' => $user->id(),
      'name' => $user->getAccountName(),
      'type' => 'page',
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ];
    $node1 = $this->entityTypeManager()->getStorage('node')->create($values);
    $node2 = clone($node1);
    $return['node_form_1'] = $this->entityFormBuilder()->getForm($node1);
    $return['node_form_2'] = $this->entityFormBuilder()->getForm($node2);
    return $return;
  }

  /** * Emulate legacy AHAH-style ajax callback. * * Drupal 6 AHAH callbacks used to operate directly on forms retrieved using * \Drupal::formBuilder()->getCache() and stored using * \Drupal::formBuilder()->setCache() after manipulation. This callback helps * testing whether \Drupal::formBuilder()->setCache() prevents resaving of * immutable forms. */

  public function addForm(BlockContentTypeInterface $block_content_type, Request $request) {
    $block = $this->blockContentStorage->create([
      'type' => $block_content_type->id(),
    ]);
    if (($theme = $request->query->get('theme')) && in_array($themearray_keys($this->themeHandler->listInfo()))) {
      // We have navigated to this page from the block library and will keep track       // of the theme for redirecting the user to the configuration page for the       // newly created block in the given theme.       $block->setTheme($theme);
    }
    return $this->entityFormBuilder()->getForm($block);
  }

  /** * Provides the page title for this controller. * * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type * The block type being added. * * @return string * The page title. */
  

  public function addLink(MenuInterface $menu) {
    $menu_link = $this->entityTypeManager()
      ->getStorage('menu_link_content')
      ->create([
        'menu_name' => $menu->id(),
      ]);
    return $this->entityFormBuilder()->getForm($menu_link);
  }

}

  public function blockAddConfigureForm($plugin_id$theme) {
    // Create a block entity.     $entity = $this->entityTypeManager()->getStorage('block')->create(['plugin' => $plugin_id, 'theme' => $theme]);

    return $this->entityFormBuilder()->getForm($entity);
  }

}
throw new NotFoundHttpException();
        }
      }
    }

    $message = $this->entityTypeManager()
      ->getStorage('contact_message')
      ->create([
        'contact_form' => $contact_form->id(),
      ]);

    $form = $this->entityFormBuilder()->getForm($message);
    $form['#title'] = $contact_form->label();
    $form['#cache']['contexts'][] = 'user.permissions';
    $this->renderer->addCacheableDependency($form$config);
    return $form;
  }

  /** * Form constructor for the personal contact form. * * @param \Drupal\user\UserInterface $user * The account for which a personal contact form should be generated. * * @return array * The personal contact form as render array as expected by * \Drupal\Core\Render\RendererInterface::render(). * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * Exception is thrown when user tries to access a contact form for a * user who does not have an email address configured. */
/** * Returns a form to add a new term to a vocabulary. * * @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary * The vocabulary this term will be added to. * * @return array * The taxonomy term add form. */
  public function addForm(VocabularyInterface $taxonomy_vocabulary) {
    $term = $this->entityTypeManager()->getStorage('taxonomy_term')->create(['vid' => $taxonomy_vocabulary->id()]);
    return $this->entityFormBuilder()->getForm($term);
  }

  /** * Route title callback. * * @param \Drupal\taxonomy\TermInterface $taxonomy_term * The taxonomy term. * * @return array * The term label as a render array. */
  
/** * Returns a form to add a new shortcut to a given set. * * @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set * The shortcut set this shortcut will be added to. * * @return array * The shortcut add form. */
  public function addForm(ShortcutSetInterface $shortcut_set) {
    $shortcut = $this->entityTypeManager()->getStorage('shortcut')->create(['shortcut_set' => $shortcut_set->id()]);
    return $this->entityFormBuilder()->getForm($shortcut, 'add');
  }

  /** * Deletes the selected shortcut. * * @param \Drupal\shortcut\ShortcutInterface $shortcut * The shortcut to delete. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * A redirect to the previous location or the front page when destination * is not set. */
Home | Imprint | This part of the site doesn't use cookies.