/** * Updates multiple zone settings at CloudFlare. * * @param \CloudFlarePhpSdk\ApiTypes\Zone\ZoneSettings $zone * Accepts a Zone object with the changes set. * * @throws \CloudFlarePhpSdk\Exceptions\CloudFlareApiException * Throws an exception if there is an application level error returned from * the API. */ public function updateZone(ZoneSettings $zone) { $request_path = strtr('zones/:zone_identifier/settings', [':zone_identifier' => $zone->getId()]); $changed_settings = $zone->getChangedResults(); $this->makeRequest(self::REQUEST_TYPE_PATCH, $request_path, $changed_settings); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_user_input = $form_state->getValue('table'); $zone_id = $form_state->getValue('selected'); // @todo. Might be worth saving this in the form_state so we don't need // to load this 2x. $this->config = \Drupal::config('cloudflare.settings'); $zone_api = new ZoneApi($this->config->get('apikey'), $this->config->get('email')); $zone_settings = $zone_api->getZoneSettings($zone_id); foreach ($form_user_input as $setting_name => $value_wrapper) { $current_setting = $zone_settings->getSettingById($setting_name); $is_boolean = in_array($setting_name, ZoneSettings::getBooleanSettings()); $is_integer = in_array($setting_name, ZoneSettings::getIntegerSettings()); $is_select = in_array($setting_name, ZoneSettings::getSelectSettings()); // If the setting is not editable then there is nothing to change on the // API. if (!$current_setting->isEditable()) { continue; } if ($is_boolean) { $new_value = $value_wrapper['value'][0]; $value_changed = $new_value != $current_setting->getValue(); if ($value_changed) { $current_setting->setValue($new_value ? 'on' : 'off'); } } elseif ($is_integer) { $new_value = $value_wrapper['value'][0]; $value_changed = $new_value != $current_setting->getValue(); if ($value_changed) { $current_setting->setValue($new_value); } } elseif ($is_select) { $new_value = $value_wrapper['value'][0][$setting_name]; $value_changed = $new_value != $current_setting->getValue(); if ($value_changed) { $current_setting->setValue($new_value); } } else { switch ($setting_name) { case ZoneSettings::SETTING_MINIFY: $new_css = $value_wrapper['value'][0][ZoneSettings::SETTING_MINIFY_CSS]; $new_js = $value_wrapper['value'][0][ZoneSettings::SETTING_MINIFY_JS]; $new_html = $value_wrapper['value'][0][ZoneSettings::SETTING_MINIFY_HTML]; $old_css = $current_setting->isCssMinifyEnabled(); $old_js = $current_setting->isJsMinifyEnabled(); $old_html = $current_setting->isHtmlMinifyEnabled(); $value_changed = $new_css != $old_css || $new_js != $old_js || $new_html != $old_html; if ($value_changed) { $current_setting->setValue($new_css, $new_html, $new_js); } break; // @todo this has not been wired up yet. // @todo this has not been wired up yet. case ZoneSettings::SETTING_MOBILE_REDIRECT: $is_mobile_redirect_enabled = (bool) $value[ZoneSettings::SETTING_MOBILE_REDIRECT_ENABLED]; $mobile_subdomain = $value[ZoneSettings::SETTING_MOBILE_REDIRECT_MOBILE_SUBDOMAIN]; $is_strip_uri_enabled = (bool) $value[ZoneSettings::SETTING_MOBILE_REDIRECT_STRIP_URI]; break; // @todo this has not been wired up yet. // @todo this has not been wired up yet. case ZoneSettings::SETTING_SECURITY_HEADER: break; } } } try { $zone_api->updateZone($zone_settings); } catch (CloudFlareHttpException $e) { drupal_set_message("Unable to connect to CloudFlare. " . $e->getMessage(), 'error'); \Drupal::logger('cloudflare')->error($e->getMessage()); $form['zone']['#access'] = FALSE; return; } catch (CloudFlareApiException $e) { drupal_set_message("Unable to connect to CloudFlare. " . $e->getMessage(), 'error'); \Drupal::logger('cloudflare')->error($e->getMessage()); return; } drupal_set_message($this->t('The configuration options have been saved.')); }