You are here

class RelatedProperties in Salesforce Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedProperties
  2. 5.0.x modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php \Drupal\salesforce_mapping\Plugin\SalesforceMappingField\RelatedProperties

Adapter for entity Reference and fields.

Plugin annotation


@Plugin(
  id = "RelatedProperties",
  label = @Translation("Related Entity Properties")
)

Hierarchy

Expanded class hierarchy of RelatedProperties

File

modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedProperties.php, line 19

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField
View source
class RelatedProperties extends SalesforceMappingFieldPluginBase {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $pluginForm = parent::buildConfigurationForm($form, $form_state);

    // @TODO inspecting the form and form_state feels wrong, but haven't found a good way to get the entity from config before the config is saved.
    $options = $this
      ->getConfigurationOptions($form['#entity']);
    if (empty($options)) {
      $pluginForm['drupal_field_value'] += [
        '#markup' => t('No available entity reference fields.'),
      ];
    }
    else {
      $pluginForm['drupal_field_value'] += [
        '#type' => 'select',
        '#options' => $options,
        '#empty_option' => $this
          ->t('- Select -'),
        '#default_value' => $this
          ->config('drupal_field_value'),
        '#description' => $this
          ->t('Select a property from the referenced field.<br />If more than one entity is referenced, the entity at delta zero will be used.<br />An entity reference field will be used to sync an identifier, e.g. Salesforce ID and Node ID.'),
      ];
    }
    return $pluginForm;
  }

  /**
   * {@inheritdoc}
   */
  public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
    list($field_name, $referenced_field_name) = explode(':', $this
      ->config('drupal_field_value'), 2);

    // Since we're not setting hard restrictions around bundles/fields, we may
    // have a field that doesn't exist for the given bundle/entity. In that
    // case, calling get() on an entity with a non-existent field argument
    // causes an exception during entity save. Probably a bug, but I haven't
    // found it in the issue queue. So, just check first to make sure the field
    // exists.
    $instances = $this->entityFieldManager
      ->getFieldDefinitions($mapping
      ->get('drupal_entity_type'), $mapping
      ->get('drupal_bundle'));
    if (empty($instances[$field_name])) {
      return;
    }
    $field = $entity
      ->get($field_name);
    if (empty($field->entity)) {

      // This reference field is blank.
      return;
    }
    try {
      $describe = $this->salesforceClient
        ->objectDescribe($mapping
        ->getSalesforceObjectType());
      $field_definition = $describe
        ->getField($this
        ->config('salesforce_field'));
      if ($field_definition['type'] == 'multipicklist') {
        $values = [];
        foreach ($field as $ref_entity) {
          $values[] = $ref_entity->entity
            ->get($referenced_field_name)->value;
        }
        return implode(';', $values);
      }
      else {
        return $field->entity
          ->get($referenced_field_name)->value;
      }
    } catch (\Exception $e) {
      return NULL;
    }
  }

  /**
   * Form options helper.
   */
  protected function getConfigurationOptions($mapping) {
    $instances = $this->entityFieldManager
      ->getFieldDefinitions($mapping
      ->get('drupal_entity_type'), $mapping
      ->get('drupal_bundle'));
    if (empty($instances)) {
      return;
    }
    $options = [];

    // Loop over every field on the mapped entity. For reference fields, expose
    // all properties of the referenced entity.
    foreach ($instances as $instance) {
      if (!$this
        ->instanceOfEntityReference($instance)) {
        continue;
      }
      $settings = $this
        ->selectionPluginManager()
        ->getSelectionHandler($instance)
        ->getConfiguration();
      $entity_type = $settings['target_type'];
      $properties = [];

      // If handler is default and allowed bundles are set, include all fields
      // from all allowed bundles.
      try {
        if (!empty($settings['handler_settings']['target_bundles'])) {
          foreach ($settings['handler_settings']['target_bundles'] as $bundle) {
            $properties += $this->entityFieldManager
              ->getFieldDefinitions($entity_type, $bundle);
          }
        }
        else {
          $properties += $this->entityFieldManager
            ->getBaseFieldDefinitions($entity_type);
          $bundles = array_keys($this->entityTypeBundleInfo
            ->getBundleInfo($entity_type));
          foreach ($bundles as $bundle) {
            $properties += $this->entityFieldManager
              ->getFieldDefinitions($entity_type, $bundle);
          }
        }
      } catch (\Exception $e) {

        // @TODO is there a better way to exclude non-fieldables?
        continue;
      }
      foreach ($properties as $key => $property) {
        $options[(string) $instance
          ->getLabel()][$instance
          ->getName() . ':' . $key] = $property
          ->getLabel();
      }
    }
    if (empty($options)) {
      return;
    }

    // Alphabetize options for UI.
    foreach ($options as $group => &$option_set) {
      asort($option_set);
    }
    asort($options);
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RelatedProperties::buildConfigurationForm public function Form constructor. Overrides SalesforceMappingFieldPluginBase::buildConfigurationForm
RelatedProperties::getConfigurationOptions protected function Form options helper.
RelatedProperties::value public function Given a Drupal entity, return the outbound value. Overrides SalesforceMappingFieldPluginInterface::value
SalesforceMappingFieldPluginBase::$entityFieldManager protected property Entity field manager service.
SalesforceMappingFieldPluginBase::$entityTypeBundleInfo protected property Entity type bundle info service.
SalesforceMappingFieldPluginBase::$entityTypeManager protected property Entity type manager service.
SalesforceMappingFieldPluginBase::$eventDispatcher protected property Event dispatcher service.
SalesforceMappingFieldPluginBase::$id protected property The machine name of the mapping.
SalesforceMappingFieldPluginBase::$label protected property The label of the mapping.
SalesforceMappingFieldPluginBase::$mappedObjectStorage protected property Storage handler for Mapped Objects.
SalesforceMappingFieldPluginBase::$mappingStorage protected property Storage handler for SF mappings.
SalesforceMappingFieldPluginBase::$salesforceClient protected property Salesforce client service.
SalesforceMappingFieldPluginBase::buildBrokenConfigurationForm protected function Helper for buildConfigurationForm() to build a broken field plugin.
SalesforceMappingFieldPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
SalesforceMappingFieldPluginBase::config public function In order to set a config value to null, use setConfiguration() Overrides SalesforceMappingFieldPluginInterface::config
SalesforceMappingFieldPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
SalesforceMappingFieldPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration
SalesforceMappingFieldPluginBase::get public function Used for returning values by key. Overrides SalesforceMappingFieldPluginInterface::get
SalesforceMappingFieldPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
SalesforceMappingFieldPluginBase::getDependencies public function Return an array of dependencies. Overrides SalesforceMappingFieldPluginInterface::getDependencies 1
SalesforceMappingFieldPluginBase::getDrupalFieldType protected function Helper method to get the Field Type of the given Field Data Definition. 1
SalesforceMappingFieldPluginBase::getFieldDataDefinition protected function Helper method to get the Data Definition for the current field. 1
SalesforceMappingFieldPluginBase::getSalesforceFieldOptions protected function Helper to retreive a list of fields for a given object type.
SalesforceMappingFieldPluginBase::instanceOfEntityReference protected function Return TRUE if the given field uses an entity reference handler.
SalesforceMappingFieldPluginBase::isAllowed public static function Determine whether this plugin is allowed for a given mapping. Overrides SalesforceMappingFieldPluginInterface::isAllowed 4
SalesforceMappingFieldPluginBase::label public function Returns label of the mapping field plugin. Overrides SalesforceMappingFieldPluginInterface::label
SalesforceMappingFieldPluginBase::pull public function Whether this plugin supports "pull" operations. Overrides SalesforceMappingFieldPluginInterface::pull 5
SalesforceMappingFieldPluginBase::pullValue public function Pull callback for field plugins. Overrides SalesforceMappingFieldPluginInterface::pullValue 2
SalesforceMappingFieldPluginBase::push public function Whether this plugin supports "push" operations. Overrides SalesforceMappingFieldPluginInterface::push
SalesforceMappingFieldPluginBase::pushValue public function Munge the value that's being prepared to push to Salesforce. Overrides SalesforceMappingFieldPluginInterface::pushValue
SalesforceMappingFieldPluginBase::selectionPluginManager protected function Wraper for plugin.manager.entity_reference_selection service.
SalesforceMappingFieldPluginBase::set public function Used for returning values by key. Overrides SalesforceMappingFieldPluginInterface::set
SalesforceMappingFieldPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration
SalesforceMappingFieldPluginBase::submitConfigurationForm public function Implements PluginFormInterface::submitConfigurationForm(). Overrides PluginFormInterface::submitConfigurationForm 1
SalesforceMappingFieldPluginBase::validateConfigurationForm public function Implements PluginFormInterface::validateConfigurationForm(). Overrides PluginFormInterface::validateConfigurationForm 2
SalesforceMappingFieldPluginBase::__construct public function SalesforceMappingFieldPluginBase constructor. Overrides PluginBase::__construct 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.