Score:0

Web form Custom element

jp flag

I created a custom webform element, It works perfectly, but when I add a table form element, It is not showing enter image description here

My Code :

    <?php

namespace Drupal\webform_mapper\Plugin\WebformElement;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Provides a 'webform_example_element' element.
 *
 * @WebformElement(
 *   id = "webform_example_element",
 *   label = @Translation("Webform example element"),
 *   description = @Translation("Provides a webform element example."),
 *   category = @Translation("Example elements"),
 * )
 *
 * @see \Drupal\webform_example_element\Element\WebformExampleElement
 * @see \Drupal\webform\Plugin\WebformElementBase
 * @see \Drupal\webform\Plugin\WebformElementInterface
 * @see \Drupal\webform\Annotation\WebformElement
 */
class WebformExampleElement extends WebformElementBase {

  use DependencySerializationTrait;

  /**
   * {@inheritdoc}
   */
  protected function defineDefaultProperties() {
    return [
        'multiple' => '',
        'size' => '',
        'minlength' => '',
        'maxlength' => '',
        'placeholder' => '',
        'source_webform' => '',
        'target_elements' => [],
        'reference_element' => [],
        'reference_element_2' => [],
      ] + parent::defineDefaultProperties();
  }

  /* ************************************************************************ */

  /**
   * {@inheritdoc}
   */
  public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
    parent::prepare($element, $webform_submission);

    // Here you can customize the webform element's properties.
    // You can also customize the form/render element's properties via the
    // FormElement.
    //
    // @see \Drupal\webform_example_element\Element\WebformExampleElement::processWebformElementExample
  }

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

    $form['mapped_container'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Auto complete data element settings'),
      '#open' => TRUE,
      '#weight' => -10,
      '#attributes' => [
        'id' => ['webform-mapped-container'],
      ],
    ];

    $query = \Drupal::entityQuery('webform');
    $results = $query->execute();

    $form['mapped_container']['source_webform'] = [
      '#type' => 'select',
      '#title' => $this->t('Source webform'),
      '#options' => $results,
      '#ajax' => [
        'callback' => [$this, 'updateFormCallback'],
        'wrapper' => 'webform-mapped-container',
      ],
    ];

    $id = $form_state->getValue('source_webform') != NULL ? $form_state->getValue('source_webform') : '';
    if (!empty($id)) {
      $form['mapped_container']['reference_element'] = [
        '#type' => 'select',
        '#title' => $this->t('Reference Element'),
        '#options' => ['t1', 't2'],
      ];
      $form['mapped_container']['reference_element_2'] = [
        '#type' => 'select',
        '#title' => $this->t('Reference Element'),
        '#options' => ['t5', 't2'],
      ];

      $form['mapped_container']['target_elements'] = [
        '#type' => 'table',
        '#title' => $this->t('Map Source Elements to Target Elements'),
        '#header' => [
          $this
            ->t('Source Webform Elements'),
          $this
            ->t('Target Webform Elements'),
        ],
      ];

      for ($i = 1; $i <= 4; $i++) {
        $form['mapped_container']['target_elements'][$i]['name'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Name'),
          '#title_display' => 'invisible',
        ];
        $form['mapped_container']['target_elements'][$i]['phone'] = [
          '#type' => 'tel',
          '#title' => $this
            ->t('Phone'),
          '#title_display' => 'invisible',
        ];
      }

    }

    return $form;
  }

  public function updateFormCallback(array $form, FormStateInterface $form_state) {
    $id = $form_state->getValues()['properties']['source_webform'];
    $form_state->setValue('source_webform', $id);
    $triggering_element = $form_state->getTriggeringElement();
    $parents = array_slice($triggering_element['#array_parents'], 0, -1);
    $element = NestedArray::getValue($form, $parents);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function preview() {
    return [];
  }

}
Score:0
jp flag

Solution : add the #access attribute

$form['mapped_container']['target_elements'] = [
      '#type' => 'table',
      '#title' => $this->t('Map Source Elements to Target Elements'),
      '#header' => [
        $this
          ->t('Source Webform Elements'),
        $this
          ->t('Target Webform Elements'),
      ],
      '#access' => TRUE,
    ];

    for ($i = 1; $i <= 4; $i++) {
      $form['mapped_container']['target_elements'][$i] = [
        '#access' => TRUE,
        'c1' => [
          '#access' => TRUE,
          '#type' => 'textfield',
          '#title' => $this->t('Name'),
        ],
        'c2' => [
          '#access' => TRUE,
          '#type' => 'tel',
          '#title' => $this->t('Phone'),
          '#title_display' => 'invisible',
        ],
      ];
    }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.