diff --git a/src/Form/HubspotSettingsForm.php b/src/Form/HubspotSettingsForm.php index d3cf24d..a5474e4 100644 --- a/src/Form/HubspotSettingsForm.php +++ b/src/Form/HubspotSettingsForm.php @@ -80,6 +80,48 @@ class HubspotSettingsForm extends ConfigFormBase { ], ]; + $form['hubspot_access_token_options'] = array( + '#type' => 'fieldset', + '#title' => t('Default query options'), + '#description' => $this->t('Refer to the @formapi.', [ + '@formapi' => Link::fromTextAndUrl($this->t('Marketing API Forms documentation'), Url::fromUri('https://developers.hubspot.com/docs/api/marketing/forms#tab-2'))->toString(), + ]), + '#tree' => TRUE, + '#collapsible' => FALSE, + '#collapsed' => FALSE, + '#states' => [ + 'visible' => [':input[name="hubspot_access_type"]' => ['value' => 1]], + ], + ); + + $form['hubspot_access_token_options']['after'] = [ + '#type' => 'textfield', + '#title' => $this->t('After'), + '#default_value' => $config->get('hubspot_access_token_options.after'), + '#description' => $this->t('The paging cursor token of the last successfully read resource will be returned as the paging.next.after JSON property of a paged response containing more results.'), + ]; + + $form['hubspot_access_token_options']['limit'] = [ + '#type' => 'textfield', + '#title' => $this->t('Limit'), + '#default_value' => $config->get('hubspot_access_token_options.limit'), + '#description' => $this->t('The maximum number of results to display per page. 20 if not set. 2147483646 max'), + ]; + + $form['hubspot_access_token_options']['archived'] = [ + '#type' => 'textfield', + '#title' => $this->t('Archived'), + '#default_value' => $config->get('hubspot_access_token_options.archived'), + '#description' => $this->t('Whether to return only results that have been archived.'), + ]; + + $form['hubspot_access_token_options']['formTypes'] = [ + '#type' => 'textfield', + '#title' => $this->t('Form types'), + '#default_value' => $config->get('hubspot_access_token_options.formTypes'), + '#description' => $this->t('The form types to be included in the results. Join ids with &.'), + ]; + return parent::buildForm($form, $form_state); } @@ -96,6 +138,14 @@ class HubspotSettingsForm extends ConfigFormBase { $config->set('hubspot_access_token', $form_state->getValue('hubspot_access_token'))->save(); $config->set('hubspot_portal_id', $form_state->getValue('hubspot_portal_id'))->save(); + if($form_state->getValue('hubspot_access_type') == 1) { + $options = $form_state->getValue('hubspot_access_token_options'); + $config->set('hubspot_access_token_options.after', @$options['after'])->save(); + $config->set('hubspot_access_token_options.limit', @$options['limit'])->save(); + $config->set('hubspot_access_token_options.archived', @$options['archived'])->save(); + $config->set('hubspot_access_token_options.formTypes', @$options['formTypes'])->save(); + } + // Clear Drupal cache. \Drupal::cache()->delete('hubspot_forms'); diff --git a/src/HubspotFormsCore.php b/src/HubspotFormsCore.php index 05cf0a4..eedecab 100755 --- a/src/HubspotFormsCore.php +++ b/src/HubspotFormsCore.php @@ -38,15 +38,24 @@ class HubspotFormsCore { /** * Make call to Hubspot Forms API and get a list of all available forms. + * + * @param array $options array of options according to API documentation https://developers.hubspot.com/docs/api/marketing/forms + * @return array forms array portal_id::form_id => form_name */ - public function fetchHubspotForms() { + public function fetchHubspotForms($options = []) { $forms = []; $config = \Drupal::config('hubspot_forms.settings'); $access_type = trim($config->get('hubspot_access_type')); if (!empty($access_type)) { $access_token = $config->get('hubspot_access_token'); - $fetchedForms = $this->fetchFormsByAccessToken($access_token); + $the_options = $config->get('hubspot_access_token_options'); + $the_options = $options + $the_options; + foreach(array_keys($the_options) as $key) { + if(empty($the_options[$key])) + unset($the_options[$key]); + } + $fetchedForms = $this->fetchFormsByAccessToken($access_token, $the_options); usort($fetchedForms, function ($a, $b) { return strtotime($b->createdAt) - strtotime($a->createdAt); }); @@ -77,17 +86,19 @@ class HubspotFormsCore { * * @param string $access_token * Access Token. + * + * @param array $options + * array of options according to API documentation https://developers.hubspot.com/docs/api/marketing/forms * * @return array * Array of Forms */ - private function fetchFormsByAccessToken($access_token) { + private function fetchFormsByAccessToken($access_token, $options = []) { try { - // Get all forms from a portal. - // See https://developers.hubspot.com/docs/api/marketing/forms $uri = 'https://api.hubapi.com/marketing/v3/forms/'; $client = \Drupal::httpClient(); $request = $client->request('GET', $uri, [ + 'query' => $options, 'timeout' => 5, 'headers' => [ 'Accept' => 'application/json',