You are here

class ServicesClientFieldFormatter in Services Client 7.2

Hierarchy

Expanded class hierarchy of ServicesClientFieldFormatter

6 string references to 'ServicesClientFieldFormatter'
ServicesClientErrorWebTestCase::testServicesClientErrors in services_client_error/tests/services_client_error.test
ServicesClientHooksWebTestCase::testServicesClientHooks in tests/services_client.test
ServicesClientWebTestCase::testServicesClientProcessing in tests/services_client.test
services_client_migrate_add_mapping in ./services_client.legacy.inc
Create mapping plugins by old configurration.
services_client_services_client_mapping in ./services_client.plugins.inc
List available mapping plugins.

... See full list

File

include/mapping.inc, line 625

View source
class ServicesClientFieldFormatter extends ServicesClientMapperPlugin implements ServicesClientMappingFormatterInterface {

  /**
   * Retrieve default configuration.
   *
   * @return array
   *   Default configuration.
   */
  protected function getDefaultConfiguration() {
    return array(
      'field' => '',
      'language' => LANGUAGE_NONE,
      'property' => '',
      'multivalue' => 'all_values',
      'empty' => 'no_field',
      'default_value' => '',
    );
  }

  /**
   * Get pluginc configuration summary.
   *
   * @return string
   *   Configuration overview string.
   */
  public function getSummary() {
    if (empty($this->config['field'])) {
      return '[FieldFormatter - not configured]';
    }
    else {
      $delta = $this->config['multivalue'] == 'force_single' ? '0' : '*';
      return "\$object-><b>{$this->config['field']}[{$this->config['language']}][{$delta}][{$this->config['property']}]</b>";
    }
  }

  /**
   * Retrieve empty value formatting by configuration.
   *
   * @return array|NULL
   *   Formatted empty value.
   */
  protected function formatEmptyValue() {
    if ($this->config['empty'] == 'no_field') {
      return NULL;
    }
    elseif ($this->config['empty'] == 'null_field') {
      return array(
        'key' => $this->config['field'],
        'value' => array(
          $this->config['language'] => array(),
        ),
      );
    }
    elseif ($this->config['empty'] == 'null_property') {
      return array(
        'key' => $this->config['field'],
        'value' => NULL,
      );
    }
    elseif ($this->config['empty'] == 'default_value') {
      return array(
        'key' => $this->config['field'],
        'value' => array(
          $this->config['language'] => array(
            array(
              $this->config['property'] => $this->config['default_value'],
            ),
          ),
        ),
      );
    }
  }

  /**
   * Format source value array to field.
   *
   * @param array $values
   *   Source values that should be formatted.
   *
   * @return array
   *   Formatted value in format
   */
  protected function formatArray($values) {

    // Make handy shortcuts
    $language = $this->config['language'];
    $field = $this->config['field'];
    $property = $this->config['property'];

    // Build final value
    $out = array();
    foreach ($values as $value) {
      $out[$language][][$property] = $value;
    }

    // Return resulting value.
    return array(
      'key' => $this->config['field'],
      'value' => $out,
    );
  }

  /**
   * Format source value to destination.
   *
   * @param ServicesClientMappingValue $source
   *   Source value gathered from Reader.
   *
   * @return array
   *   Value specified by 'key' => 'name', 'value' => 'mixed'
   */
  public function format(ServicesClientMappingValue $source) {

    // Handle empty values
    if ($source
      ->isEmpty()) {
      return $this
        ->formatEmptyValue();
    }

    // If single value is forced.
    $values = $source
      ->getValue();
    if ($this->config['multivalue'] == 'force_single') {
      $values = array(
        reset($values),
      );
    }
    return $this
      ->formatArray($values);
  }

  /**
   * Config form.
   */
  public function configForm(&$form, &$form_state) {
    $form['formatter_config'] = array(
      '#type' => 'fieldset',
      '#title' => t('Property reader'),
      '#tree' => TRUE,
    );
    $form['formatter_config']['field'] = array(
      '#type' => 'textfield',
      '#title' => t('Field name'),
      '#description' => t('Enter field name'),
      '#default_value' => $this->config['field'],
    );
    $form['formatter_config']['language'] = array(
      '#type' => 'textfield',
      '#title' => t('Language'),
      '#description' => t('Enter field name'),
      '#default_value' => $this->config['language'],
    );
    $form['formatter_config']['property'] = array(
      '#type' => 'textfield',
      '#title' => t('Property name'),
      '#description' => t('Enter property name'),
      '#default_value' => $this->config['property'],
    );
    $form['formatter_config']['multivalue'] = array(
      '#type' => 'select',
      '#title' => t('Multiple values'),
      '#description' => t('How to handle multiple values'),
      '#options' => array(
        'force_single' => t('Always force single value'),
        'all_values' => t('Send all values available in field'),
      ),
      '#default_value' => $this->config['multivalue'],
    );
    $form['formatter_config']['empty'] = array(
      '#type' => 'select',
      '#title' => t('Empty values'),
      '#description' => t('How to handle empty values'),
      '#options' => array(
        'no_field' => t("Don't create any field"),
        'null_field' => t("Create null field (deletes remote value)."),
        'default_value' => t('Provide default value'),
      ),
      '#default_value' => $this->config['empty'],
    );
    $form['formatter_config']['default_value'] = array(
      '#type' => 'textfield',
      '#title' => t('Default value'),
      '#description' => t('Provide default value if reader will provide empty value.'),
      '#default_value' => $this->config['default_value'],
      '#states' => array(
        'visible' => array(
          ':input[name="formatter_config[empty]"]' => array(
            array(
              'value' => 'default_value',
            ),
          ),
        ),
      ),
    );
  }

  /**
   * Submit configuration form.
   */
  public function configFormSubmit(&$form, &$form_state) {
    $this->config['field'] = $form_state['values']['formatter_config']['field'];
    $this->config['language'] = $form_state['values']['formatter_config']['language'];
    $this->config['property'] = $form_state['values']['formatter_config']['property'];
    $this->config['multivalue'] = $form_state['values']['formatter_config']['multivalue'];
    $this->config['empty'] = $form_state['values']['formatter_config']['empty'];
    $this->config['default_value'] = $form_state['values']['formatter_config']['default_value'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServicesClientFieldFormatter::configForm public function Config form. Overrides ServicesClientMapperPlugin::configForm 1
ServicesClientFieldFormatter::configFormSubmit public function Submit configuration form. Overrides ServicesClientMapperPlugin::configFormSubmit 1
ServicesClientFieldFormatter::format public function Format source value to destination. Overrides ServicesClientMappingFormatterInterface::format
ServicesClientFieldFormatter::formatArray protected function Format source value array to field. 1
ServicesClientFieldFormatter::formatEmptyValue protected function Retrieve empty value formatting by configuration. 1
ServicesClientFieldFormatter::getDefaultConfiguration protected function Retrieve default configuration. Overrides ServicesClientMapperPlugin::getDefaultConfiguration
ServicesClientFieldFormatter::getSummary public function Get pluginc configuration summary. Overrides ServicesClientMappingInterface::getSummary 1
ServicesClientMapperPlugin::configFormValidate public function Validate configuration form. Overrides ServicesClientPlugin::configFormValidate
ServicesClientMapperPlugin::__construct public function Constructor. Overrides ServicesClientPlugin::__construct
ServicesClientPlugin::$config protected property Plugin specific configuration
ServicesClientPlugin::$event protected property Event definition
ServicesClientPlugin::getConfiguration public function Retrieve current plugin configuration. Overrides ServicesClientConfigurableInterface::getConfiguration
ServicesClientPlugin::setConfiguration public function Set configuration of plugin. Overrides ServicesClientConfigurableInterface::setConfiguration