Commit b5054cce authored by Jacob Rockowitz's avatar Jacob Rockowitz Committed by Jacob Rockowitz
Browse files

Issue #3252811 by jrockowitz, lamp5: Schema for match_limit and webforms...

Issue #3252811 by jrockowitz, lamp5: Schema for match_limit and webforms parameters are missing in entity reference autocomplete widget
parent 120981a6
Loading
Loading
Loading
Loading
+3 −0
Changes for config/schema/webform.field.schema.yml: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -48,6 +48,9 @@ field.widget.settings.webform_entity_reference_autocomplete:
  type: mapping
  label: 'Webform autocomplete display format settings'
  mapping:
    match_limit:
      type: integer
      label: 'Match limit'
    match_operator:
      type: string
      label: 'Autocomplete matching'
+32 −0
Changes for src/Plugin/Field/FieldWidget/WebformEntityReferenceAutocompleteWidget.php: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,38 @@ class WebformEntityReferenceAutocompleteWidget extends EntityReferenceAutocomple

  use WebformEntityReferenceWidgetTrait;

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'default_data' => TRUE,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    $element['default_data'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable default submission data (YAML)'),
      '#description' => $this->t('If checked, site builders will be able to define default submission data (YAML)'),
      '#default_value' => $this->getSetting('default_data'),
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $summary[] = $this->t('Default submission data: @default_data', ['@default_data' => $this->getSetting('default_data') ? $this->t('Yes') : $this->t('No')]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
+61 −9
Changes for src/Plugin/Field/FieldWidget/WebformEntityReferenceSelectWidget.php: 61 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -37,6 +37,43 @@ class WebformEntityReferenceSelectWidget extends OptionsWidgetBase {
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    $element['default_data'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable default submission data (YAML)'),
      '#description' => $this->t('If checked, site builders will be able to define default submission data (YAML)'),
      '#default_value' => $this->getSetting('default_data'),
    ];
    $element['webforms'] = [
      '#type' => 'webform_entity_select',
      '#title' => $this->t('Select webform'),
      '#description' => $this->t('If left blank all webforms will be listed in the select menu.'),
      '#select2' => TRUE,
      '#multiple' => TRUE,
      '#target_type' => 'webform',
      '#selection_handler' => 'default:webform',
      '#default_value' => $this->getSetting('webforms'),
    ];
    $this->elementManager->processElement($element['webforms']);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $summary[] = $this->t('Default submission data: @default_data', ['@default_data' => $this->getSetting('default_data') ? $this->t('Yes') : $this->t('No')]);
    if ($webform_options = $this->getWebformsAsOptions()) {
      $summary[] = $this->t('Webforms: @webforms', ['@webforms' => implode('; ', $webform_options)]);
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
@@ -99,14 +136,9 @@ class WebformEntityReferenceSelectWidget extends OptionsWidgetBase {
   */
  protected function getOptions(FieldableEntityInterface $entity) {
    if (!isset($this->options)) {
      $webform_ids = $this->getSetting('webforms');
      if ($webform_ids) {
        $webforms = Webform::loadMultiple($webform_ids);
        $options = [];
        foreach ($webforms as $webform) {
          $options[$webform->id()] = $webform->label();
        }
        asort($options);
      $webform_options = $this->getWebformsAsOptions();
      if ($webform_options) {
        $options = $webform_options;
      }
      else {
        // Limit the settable options for the current user account.
@@ -117,7 +149,6 @@ class WebformEntityReferenceSelectWidget extends OptionsWidgetBase {
          ->getFieldStorageDefinition()
          ->getOptionsProvider($this->column, $entity)
          ->getSettableOptions(\Drupal::currentUser());

      }

      $module_handler = \Drupal::moduleHandler();
@@ -133,4 +164,25 @@ class WebformEntityReferenceSelectWidget extends OptionsWidgetBase {
    return $this->options;
  }

  /**
   * Get selected webforms as options.
   *
   * @return array
   *   Webforms as options.
   */
  protected function getWebformsAsOptions() {
    $webform_ids = $this->getSetting('webforms');
    if (empty($webform_ids)) {
      return [];
    }

    $webforms = Webform::loadMultiple($webform_ids);
    $options = [];
    foreach ($webforms as $webform) {
      $options[$webform->id()] = $webform->label();
    }
    asort($options);
    return $options;
  }

}
+0 −55
Changes for src/Plugin/Field/FieldWidget/WebformEntityReferenceWidgetTrait.php: 0 added lines, 55 removed lines.
Original line number Diff line number Diff line
@@ -34,61 +34,6 @@ trait WebformEntityReferenceWidgetTrait {
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'default_data' => TRUE,
      'webforms' => NULL,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    $element['default_data'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable default submission data (YAML)'),
      '#description' => $this->t('If checked, site builders will be able to define default submission data (YAML)'),
      '#default_value' => $this->getSetting('default_data'),
    ];
    if ($this->getSetting('webforms') !== NULL) {
      $element['webforms'] = [
        '#type' => 'webform_entity_select',
        '#title' => $this->t('Select webform'),
        '#description' => $this->t('If left blank all webforms will be listed in the select menu.'),
        '#select2' => TRUE,
        '#multiple' => TRUE,
        '#target_type' => 'webform',
        '#selection_handler' => 'default:webform',
        '#default_value' => $this->getSetting('webforms'),
      ];
      $this->elementManager->processElement($element['webforms']);
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $summary[] = $this->t('Default submission data: @default_data', ['@default_data' => $this->getSetting('default_data') ? $this->t('Yes') : $this->t('No')]);
    $webform_ids = $this->getSetting('webforms');
    if ($webform_ids) {
      $webforms = Webform::loadMultiple($webform_ids);
      $webform_labels = [];
      foreach ($webforms as $webform) {
        $webform_labels[] = $webform->label();
      }
      $summary[] = $this->t('Webforms: @webforms', ['@webforms' => implode('; ', $webform_labels)]);
    }
    return $summary;
  }

  /**
   * Returns the target id element form for a single webform field widget.
   *