You are here

public function CampaignMonitor::getCustomFields in Campaign Monitor 8

Fetch custom fields for a given list, then store this information locally in the list cache. The information is stored as a keyed array on the list array under the "CustomFields" key.

Parameters

string $listId: The Campaign Monitor list ID.

Return value

mixed array | FALSE An array with the information or FALSE on failure.

1 call to CampaignMonitor::getCustomFields()
CampaignMonitor::getExtendedList in src/CampaignMonitor.php
Get all information available about a given list. This is done by calling getLists(), getListDetails() and getCustomFields(), hence building the local list cache.

File

src/CampaignMonitor.php, line 363
Implementation of the CampaignMonitor class, which is a wrapper class for Campaign Monitor v3 API. It's implemented as a Singleton class and instances are created using the account variables and the static function getConnector(). An example:.

Class

CampaignMonitor

Namespace

Drupal\campaignmonitor

Code

public function getCustomFields($listId) {

  // If the lists have not been loaded yet, get them as they build the basic
  // cache.
  if (empty($this->lists)) {
    $this
      ->getLists();
  }

  // Test that the listId is valid.
  if (!isset($this->lists[$listId])) {
    $this
      ->addError(WATCHDOG_ERROR, t('Unknown list id @listID.', [
      '@listID' => $listId,
    ]));
    return FALSE;
  }

  // If custom fields are not set on the list, then create the list object and
  // fetch custom fields into a keyed array.
  if (!isset($this->lists[$listId]['CustomFields'])) {
    if ($obj = $this
      ->createListObj($listId)) {
      $result = $obj
        ->get_custom_fields();
      if ($result
        ->was_successful()) {
        $this->lists[$listId]['CustomFields'] = [];
        foreach ($result->response as $field) {
          foreach ($field as $name => $details) {
            $this->lists[$listId]['CustomFields'][$field->Key][$name] = $details;
          }
        }

        // Update cache with list details.
        \Drupal::cache()
          ->set('campaignmonitor_lists', $this->lists);
      }
      else {
        $this
          ->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
      }
    }
    else {
      return FALSE;
    }
  }
  return $this->lists[$listId]['CustomFields'];
}