buildForm example


  public function getConfirmText() {
    return $this->t('Delete');
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state, TermInterface $taxonomy_term = NULL) {
    $this->taxonomyTerm = $taxonomy_term;

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->taxonomyTerm->delete();
    $this->messenger()->addStatus($this->t('The forum %label and all sub-forums have been deleted.', ['%label' => $this->taxonomyTerm->label()]));
    $this->logger('forum')->notice('forum: deleted %label and all its sub-forums.', ['%label' => $this->taxonomyTerm->label()]);
    $form_state->setRedirectUrl($this->getCancelUrl());
  }

}
/** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if ($this->entity->getTypePlugin()->workflowHasData($this->entity)) {
      $form['#title'] = $this->getQuestion();
      $form['description'] = ['#markup' => $this->t('This workflow is in use. You cannot remove this workflow until you have removed all content using it.')];
      return $form;
    }

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete %name?', ['%name' => $this->entity->label()]);
  }

  /** * {@inheritdoc} */

  public function buildForm(array $form, FormStateInterface $form_state$ban_id = '') {
    if (!$this->banIp = $this->ipManager->findById($ban_id)) {
      throw new NotFoundHttpException();
    }
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->ipManager->unbanIp($this->banIp);
    $this->logger('user')->notice('Deleted %ip', ['%ip' => $this->banIp]);
    $this->messenger()->addStatus($this->t('The IP address %ip was deleted.', ['%ip' => $this->banIp]));
    $form_state->setRedirectUrl($this->getCancelUrl());
  }

}

  public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
    $forum_config = $this->config('forum.settings');
    $vid = $forum_config->get('vocabulary');
    $vocabulary = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->load($vid);
    if (!$vocabulary) {
      throw new NotFoundHttpException();
    }

    // Build base taxonomy term overview.     $form = parent::buildForm($form$form_state$vocabulary);

    foreach (Element::children($form['terms']) as $key) {
      if (isset($form['terms'][$key]['#term'])) {
        /** @var \Drupal\taxonomy\TermInterface $term */
        $term = $form['terms'][$key]['#term'];
        $form['terms'][$key]['term']['#url'] = Url::fromRoute('forum.page', ['taxonomy_term' => $term->id()]);

        if (!empty($term->forum_container->value)) {
          $title = $this->t('edit container');
          $url = Url::fromRoute('entity.taxonomy_term.forum_edit_container_form', ['taxonomy_term' => $term->id()]);
        }
        
'settings' => [],
    ]);

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

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    $this->sectionStorage = $section_storage;
    $form = parent::buildForm($form$form_state);
    $form['#attributes']['class'][] = 'layout-builder-form';

    // @todo \Drupal\layout_builder\Field\LayoutSectionItemList::defaultAccess()     // restricts all access to the field, explicitly allow access here until     // https://www.drupal.org/node/2942975 is resolved.     $form[OverridesSectionStorage::FIELD_NAME]['#access'] = TRUE;

    $form['layout_builder_message'] = $this->buildMessage($section_storage->getContextValue('entity')$section_storage);
    return $form;
  }

  

  }

  /** * Tests handling of disabled elements. * * @see _form_test_disabled_elements() */
  public function testDisabledElements() {
    // Get the raw form in its original state.     $form_state = new FormState();
    $form = (new FormTestDisabledElementsForm())->buildForm([]$form_state);

    // Build a submission that tries to hijack the form by submitting input for     // elements that are disabled.     $edit = [];
    foreach (Element::children($form) as $key) {
      if (isset($form[$key]['#test_hijack_value'])) {
        if (is_array($form[$key]['#test_hijack_value'])) {
          foreach ($form[$key]['#test_hijack_value'] as $subkey => $value) {
            $edit[$key . '[' . $subkey . ']'] = $value;
          }
        }
        

class SearchPageAddForm extends SearchPageFormBase {

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state$search_plugin_id = NULL) {
    $this->entity->setPlugin($search_plugin_id);
    $definition = $this->entity->getPlugin()->getPluginDefinition();
    $this->entity->set('label', $definition['title']);
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form$form_state);
    $actions['submit']['#value'] = $this->t('Save');
    return $actions;
  }

  
->save();

    // Test that the form structure includes full information for each delta     // apart from #access.     $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create(['id' => 0, 'revision_id' => 0]);

    $display = $display_repository->getFormDisplay($entity_type$entity_type);
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity$form$form_state);

    $this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');

    // Display creation form.     $this->drupalGet($entity_type . '/add');
    // Check that the widget is not displayed if field access is denied.     $this->assertSession()->fieldNotExists("{$field_name_no_access}[0][value]");

    // Create entity.     $edit = [
      "{$field_name}[0][value]" => 1,
    ];
$this->dataMapper = new DataMapper(new ChainAccessor([
            new CallbackAccessor(),
            new PropertyPathAccessor($propertyAccessor ?? PropertyAccess::createPropertyAccessor()),
        ]));
    }

    /** * @return void */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder$options);

        $isDataOptionSet = \array_key_exists('data', $options);

        $builder
            ->setRequired($options['required'])
            ->setErrorBubbling($options['error_bubbling'])
            ->setEmptyData($options['empty_data'])
            ->setPropertyPath($options['property_path'])
            ->setMapped($options['mapped'])
            ->setByReference($options['by_reference'])
            ->setInheritData($options['inherit_data'])
            

  protected function simulateFormSubmission($form_id, FormInterface $form_arg, FormStateInterface $form_state$programmed = TRUE) {
    $input = $form_state->getUserInput();
    $input['op'] = 'Submit';
    $form_state
      ->setUserInput($input)
      ->setProgrammed($programmed)
      ->setSubmitted();
    return $this->formBuilder->buildForm($form_arg$form_state);
  }

  /** * Asserts that the expected form structure is found in a form for a given key. * * @param array $expected_form * The expected form structure. * @param array $actual_form * The actual form. * @param string|null $form_key * (optional) The form key to look in. Otherwise the entire form will be * compared. */
// It is safe to implode this because theme names are not translated         // markup and so will not be double-escaped.         '@required' => implode(', ', array_map($get_label$dependencies)),
      ]);
    }
    // Add the list of experimental themes after any other messages.     $items[] = $this->t('The following themes are experimental: @themes', ['@themes' => implode(', ', array_map($get_labelarray_filter($themes$is_experimental)))]);
    $form['message'] = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $args = $form_state->getBuildInfo()['args'];
    $theme = $args[0] ?? NULL;
    $set_default = $args[1] ?? FALSE;
    $themes = $this->themeList->getList();
    $config = $this->configFactory()->getEditable('system.theme');
    
'#type' => 'textfield',
      '#title' => $this->t('Browser language code'),
      '#description' => $this->t('Use language codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', [':w3ctags' => 'http://www.w3.org/International/articles/language-tags/']),
      '#size' => 20,
    ];
    $form['new_mapping']['drupal_langcode'] = [
      '#type' => 'select',
      '#title' => $this->t('Site language'),
      '#options' => $language_options,
    ];

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Array to check if all browser language codes are unique.     $unique_values = [];

    // Check all mappings.     if ($form_state->hasValue('mappings')) {
      

  public function validateForm(array &$form, FormStateInterface $form_state) {}

  /** * Ensures maxlength attribute can be used in compatible elements. */
  public function testAttributes() {

    /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
    $form_builder = $this->container->get('form_builder');
    $form_state = new FormState();
    $elements = $form_builder->buildForm($this$form_state);
    $this->render($elements);

    $css_selector_converter = new CssSelectorConverter();
    $elements = $this->xpath($css_selector_converter->toXPath('input[name=title][maxlength=255]'));
    $this->assertCount(1, $elements, 'Text field has correct maxlength in form.');
    $elements = $this->xpath($css_selector_converter->toXPath('textarea[name=description][maxlength=255]'));
    $this->assertCount(1, $elements, 'Textarea field has correct maxlength in form.');
  }

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

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state, LanguageInterface $language = NULL) {
    if ($language) {
      $form_state->set('langcode', $language->getId());
    }
    return parent::buildForm($form$form_state);
  }

}
$build['table']['#empty'] = t('No vocabularies available.');
      }
    }

    return $build;
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form$form_state);
    $form['vocabularies']['#attributes'] = ['id' => 'taxonomy'];
    $form['actions']['submit']['#value'] = t('Save');

    return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form$form_state);

    
Home | Imprint | This part of the site doesn't use cookies.