You are here

class ServicesClientPropertyFormatter in Services Client 7.2

Formats data to property.

Hierarchy

Expanded class hierarchy of ServicesClientPropertyFormatter

7 string references to 'ServicesClientPropertyFormatter'
ServicesClientErrorWebTestCase::testServicesClientErrors in services_client_error/tests/services_client_error.test
ServicesClientHooksWebTestCase::testServicesClientHooks in tests/services_client.test
ServicesClientWebTestCase::testAddingEvent in tests/services_client.test
Test basic event configuration actions.
ServicesClientWebTestCase::testServicesClientProcessing in tests/services_client.test
services_client_migrate_add_mapping in ./services_client.legacy.inc
Create mapping plugins by old configurration.

... See full list

File

include/mapping.inc, line 495

View source
class ServicesClientPropertyFormatter extends ServicesClientMapperPlugin implements ServicesClientMappingFormatterInterface {

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

  /**
   * Get plugin configuration summary.
   *
   * @return string
   *   Configuration overview string.
   */
  public function getSummary() {
    if (empty($this->config['property'])) {
      return '[PropertyFormatter - not configured]';
    }
    else {
      return "\$object-><b>{$this->config['property']}</b>";
    }
  }

  /**
   * 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()) {
      if ($this->config['empty'] == 'no_field') {
        return NULL;
      }
      elseif ($this->config['empty'] == 'null_field') {
        return array(
          'key' => $this->config['property'],
          'value' => NULL,
        );
      }
      elseif ($this->config['empty'] == 'default_value') {
        return array(
          'key' => $this->config['property'],
          'value' => $this->config['default_value'],
        );
      }
    }

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

    // Return resulting value.
    return array(
      'key' => $this->config['property'],
      'value' => $value,
    );
  }
  public function configForm(&$form, &$form_state) {
    $form['formatter_config'] = array(
      '#type' => 'fieldset',
      '#title' => t('Property formatter'),
      '#tree' => TRUE,
    );
    $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 simple value of property'),
        'force_array' => t('Always send array even with single value'),
      ),
      '#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."),
        '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',
            ),
          ),
        ),
      ),
    );
  }
  public function configFormSubmit(&$form, &$form_state) {
    $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
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
ServicesClientPropertyFormatter::configForm public function Configuration form options Overrides ServicesClientMapperPlugin::configForm
ServicesClientPropertyFormatter::configFormSubmit public function Submit configuration form and save config. Overrides ServicesClientMapperPlugin::configFormSubmit
ServicesClientPropertyFormatter::format public function Format source value to destination. Overrides ServicesClientMappingFormatterInterface::format
ServicesClientPropertyFormatter::getDefaultConfiguration protected function Retrieve default configuration. Overrides ServicesClientMapperPlugin::getDefaultConfiguration
ServicesClientPropertyFormatter::getSummary public function Get plugin configuration summary. Overrides ServicesClientMappingInterface::getSummary