/** * Initialize the form state and the entity before the first form build. */ protected function init(FormStateInterface $form_state) { // Ensure we act on the translation object corresponding to the current form // language. $langcode = $this->getFormLangcode($form_state); $this->entity = $this->entity->getTranslation($langcode); $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation()); $this->setFormDisplay($form_display, $form_state); parent::init($form_state); }
/** * {@inheritdoc] */ public function buildForm(array $form, array &$form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) { // First entity. $form_state['entity_1'] = $entity_1; $form_state['form_display_1'] = EntityFormDisplay::collectRenderDisplay($entity_1, 'default'); $form_state['form_display_1']->buildForm($entity_1, $form, $form_state); // Second entity. $form_state['entity_2'] = $entity_2; $form_state['form_display_2'] = EntityFormDisplay::collectRenderDisplay($entity_2, 'default'); $form['entity_2'] = array('#type' => 'details', '#title' => t('Second entity'), '#tree' => TRUE, '#parents' => array('entity_2'), '#weight' => 50); $form_state['form_display_2']->buildForm($entity_2, $form['entity_2'], $form_state); $form['save'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 100); return $form; }
/** * Initialize the form state and the entity before the first form build. */ protected function init(array &$form_state, EntityInterface $entity, $field_name) { // @todo Rather than special-casing $node->revision, invoke prepareEdit() // once http://drupal.org/node/1863258 lands. if ($entity->getEntityTypeId() == 'node') { $node_type_settings = $this->nodeTypeStorage->load($entity->bundle())->getModuleSettings('node'); $options = isset($node_type_settings['options']) ? $node_type_settings['options'] : array(); $entity->setNewRevision(!empty($options['revision'])); $entity->revision_log = NULL; } $form_state['entity'] = $entity; $form_state['field_name'] = $field_name; // Fetch the display used by the form. It is the display for the 'default' // form mode, with only the current field visible. $display = EntityFormDisplay::collectRenderDisplay($entity, 'default'); foreach ($display->getComponents() as $name => $optipns) { if ($name != $field_name) { $display->removeComponent($name); } } $form_state['form_display'] = $display; }