You are here

class WebformCiviCRMSettingsForm in Webform CiviCRM Integration 8.5

Hierarchy

Expanded class hierarchy of WebformCiviCRMSettingsForm

1 string reference to 'WebformCiviCRMSettingsForm'
webform_civicrm.routing.yml in ./webform_civicrm.routing.yml
webform_civicrm.routing.yml

File

src/Form/WebformCiviCRMSettingsForm.php, line 13

Namespace

Drupal\webform_civicrm\Form
View source
class WebformCiviCRMSettingsForm extends FormBase {
  protected $webformHandlerManager;
  public function __construct(RouteMatchInterface $route_match, WebformHandlerManagerInterface $webform_handler_manager) {
    $this->routeMatch = $route_match;
    $this->webformHandlerManager = $webform_handler_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_route_match'), $container
      ->get('plugin.manager.webform.handler'));
  }

  /**
   * @return \Drupal\webform\WebformInterface
   */
  public function getWebform() {
    return $this->routeMatch
      ->getParameter('webform');
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'webform_civicrm_settings_form';
  }

  /**
   * {@inheritdoc}
   *
   * @todo slowly move parts of the D7 handling into here.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $webform = $this
      ->getWebform();
    $admin_form = \Drupal::service('webform_civicrm.admin_form')
      ->initialize($form, $form_state, $webform);
    $form = $admin_form
      ->buildForm();
    return $form;
  }

  /**
   * {@inheritdoc}
   *
   * @todo find a more elegant way to handle the handler creation/removal.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $webform = $this
      ->getWebform();
    $handler_collection = $webform
      ->getHandlers('webform_civicrm');
    $values = $form_state
      ->getValues();

    // Check if this is the confirmation form for removed fields.
    if (!isset($values['nid']) && isset($values['delete'], $values['disable'], $values['cancel'])) {
      $triggering_element = $form_state
        ->getTriggeringElement();
      if ($triggering_element['#parents'][0] === 'cancel') {
        $this
          ->messenger()
          ->addMessage('Cancelled');
        return;
      }
      if ($triggering_element['#parents'][0] === 'delete') {

        // Restore the form state values.
        $values += $form_state
          ->get('vals') ?: [];
        $form_state
          ->setValues($values);
      }
      if ($triggering_element['#parents'][0] === 'disable') {

        // @todo probably restore as well, but flag later on not to delete.
        $this
          ->messenger()
          ->addMessage('Disable is not yet supported, canceled form save.');
        return;
      }
    }
    $has_handler = $handler_collection
      ->has('webform_civicrm');
    $remove_handler = empty($values['nid']);
    if (!$has_handler && $remove_handler) {
      $this
        ->messenger()
        ->addWarning('No changes made to CiviCRM settings');
      return;
    }

    /** @var \Drupal\webform\Plugin\WebformHandlerInterface $handler */
    if (!$has_handler) {
      $handler = $this->webformHandlerManager
        ->createInstance('webform_civicrm');
      $handler
        ->setWebform($webform);
      $handler
        ->setHandlerId('webform_civicrm');
      $handler
        ->setStatus(TRUE);
      $webform
        ->addWebformHandler($handler);
    }
    else {
      $handler = $handler_collection
        ->get('webform_civicrm');
    }
    if ($remove_handler) {
      $webform
        ->deleteWebformHandler($handler);
      $this
        ->messenger()
        ->addMessage('Removed CiviCRM');
      return;
    }
    $admin_form = \Drupal::service('webform_civicrm.admin_form')
      ->initialize($form, $form_state, $webform);
    $form_state
      ->cleanValues();
    $admin_form
      ->setSettings($form_state
      ->getValues());
    $admin_form
      ->rebuildData();
    $settings = $admin_form
      ->getSettings();
    $handler_configuration = $handler
      ->getConfiguration();
    $handler_configuration['settings'] = $settings;
    $handler
      ->setConfiguration($handler_configuration);
    $admin_form
      ->postProcess();
    if (empty($admin_form->confirmPage)) {
      $webform
        ->save();
      $this
        ->messenger()
        ->addMessage('Saved CiviCRM settings');
    }
  }

  /**
   * AJAX callback for elements with a pathstr.
   *
   * @param array $form
   *   The complete form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   *
   * @return array
   *   The form element to refresh.
   */
  public static function pathstrAjaxRefresh(array &$form, FormStateInterface $form_state) {
    $triggering_element = $form_state
      ->getTriggeringElement();
    $element = NestedArray::getValue($form, explode(':', $triggering_element['#ajax']['pathstr']));
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WebformCiviCRMSettingsForm::$webformHandlerManager protected property
WebformCiviCRMSettingsForm::buildForm public function @todo slowly move parts of the D7 handling into here. Overrides FormInterface::buildForm
WebformCiviCRMSettingsForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
WebformCiviCRMSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
WebformCiviCRMSettingsForm::getWebform public function
WebformCiviCRMSettingsForm::pathstrAjaxRefresh public static function AJAX callback for elements with a pathstr.
WebformCiviCRMSettingsForm::submitForm public function @todo find a more elegant way to handle the handler creation/removal. Overrides FormInterface::submitForm
WebformCiviCRMSettingsForm::__construct public function