You are here

class RelatedIDs in Salesforce Suite 5.0.x

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

Adapter for entity Reference and fields.

Plugin annotation


@Plugin(
  id = "RelatedIDs",
  label = @Translation("Related Entity Ids")
)

Hierarchy

Expanded class hierarchy of RelatedIDs

1 string reference to 'RelatedIDs'
salesforce_mapping.salesforce_mapping.test_mapping.yml in modules/salesforce_mapping/tests/modules/salesforce_mapping_test/config/install/salesforce_mapping.salesforce_mapping.test_mapping.yml
modules/salesforce_mapping/tests/modules/salesforce_mapping_test/config/install/salesforce_mapping.salesforce_mapping.test_mapping.yml

File

modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php, line 23

Namespace

Drupal\salesforce_mapping\Plugin\SalesforceMappingField
View source
class RelatedIDs 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' => $this
          ->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('If an existing connection is found with the selected entity reference, the linked identifier will be used.<br />For example, Salesforce ID for Drupal to SF, or Node ID for SF to Drupal.<br />If more than one entity is referenced, the entity at delta zero will be used.'),
      ];
    }
    return $pluginForm;
  }

  /**
   * {@inheritdoc}
   */
  public function value(EntityInterface $entity, SalesforceMappingInterface $mapping) {
    $field_name = $this
      ->config('drupal_field_value');
    $instances = $this->entityFieldManager
      ->getFieldDefinitions($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    if (empty($instances[$field_name])) {
      return;
    }
    $field = $entity
      ->get($field_name);
    if (empty($field
      ->getValue()) || is_null($field->entity)) {

      // This reference field is blank or the referenced entity no longer
      // exists.
      return;
    }

    // Now we can actually fetch the referenced entity.
    $field_settings = $field
      ->getFieldDefinition()
      ->getSettings();

    // @TODO this procedural call will go away when sf mapping object becomes a service or field
    $referenced_mappings = $this->mappedObjectStorage
      ->loadByDrupal($field_settings['target_type'], $field->entity
      ->id());
    if (!empty($referenced_mappings)) {
      $mapping = reset($referenced_mappings);
      return $mapping
        ->sfid();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function pullValue(SObject $sf_object, EntityInterface $entity, SalesforceMappingInterface $mapping) {
    if (!$this
      ->pull() || empty($this
      ->config('salesforce_field'))) {
      throw new SalesforceException('No data to pull. Salesforce field mapping is not defined.');
    }
    $value = $sf_object
      ->field($this
      ->config('salesforce_field'));

    // Empty value means nothing to do here.
    if (empty($value)) {
      return NULL;
    }

    // If value is not an SFID, make it one.
    if (!$value instanceof SFID) {
      try {
        $value = new SFID($value);
      } catch (\Exception $e) {
        return NULL;
      }
    }

    // Convert SF Id to Drupal Id.
    $referenced_mappings = $this->mappedObjectStorage
      ->loadBySfid($value);
    if (!empty($referenced_mappings)) {
      $mapped_object = reset($referenced_mappings);
      return $mapped_object
        ->getMappedEntity() ? $mapped_object
        ->getMappedEntity()
        ->id() : NULL;
    }
  }

  /**
   * Helper to build form options.
   */
  private function getConfigurationOptions($mapping) {
    $instances = $this->entityFieldManager
      ->getFieldDefinitions($mapping
      ->get('drupal_entity_type'), $mapping
      ->get('drupal_bundle'));
    $options = [];
    foreach ($instances as $name => $instance) {
      if (!$this
        ->instanceOfEntityReference($instance)) {
        continue;
      }

      // @TODO exclude config entities?
      $options[$name] = $instance
        ->getLabel();
    }
    asort($options);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginDefinition() {
    $definition = parent::getPluginDefinition();
    $definition['config_dependencies']['config'] = [];

    // Add reference field.
    if ($field = FieldConfig::loadByName($this->mapping
      ->getDrupalEntityType(), $this->mapping
      ->getDrupalBundle(), $this
      ->config('drupal_field_value'))) {
      $definition['config_dependencies']['config'][] = $field
        ->getConfigDependencyName();

      // Add dependencies of referenced field.
      foreach ($field
        ->getDependencies() as $type => $dependency) {
        foreach ($dependency as $item) {
          $definition['config_dependencies'][$type][] = $item;
        }
      }
    }
    return $definition;
  }

  /**
   * {@inheritdoc}
   */
  public function checkFieldMappingDependency(array $dependencies) {
    $definition = $this
      ->getPluginDefinition();
    foreach ($definition['config_dependencies'] as $type => $dependency) {
      foreach ($dependency as $item) {
        if (!empty($dependencies[$type][$item])) {
          return TRUE;
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
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::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RelatedIDs::buildConfigurationForm public function Form constructor. Overrides SalesforceMappingFieldPluginBase::buildConfigurationForm
RelatedIDs::checkFieldMappingDependency public function On dependency removal, determine if this plugin needs to be removed. Overrides SalesforceMappingFieldPluginBase::checkFieldMappingDependency
RelatedIDs::getConfigurationOptions private function Helper to build form options.
RelatedIDs::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginBase::getPluginDefinition
RelatedIDs::pullValue public function Pull callback for field plugins. Overrides SalesforceMappingFieldPluginBase::pullValue
RelatedIDs::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::$mapping protected property The mapping to which this instance is attached.
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 ConfigurableInterface::defaultConfiguration
SalesforceMappingFieldPluginBase::get public function Used for returning values by key. Overrides SalesforceMappingFieldPluginInterface::get
SalesforceMappingFieldPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
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 6
SalesforceMappingFieldPluginBase::push public function Whether this plugin supports "push" operations. Overrides SalesforceMappingFieldPluginInterface::push 1
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 ConfigurableInterface::setConfiguration
SalesforceMappingFieldPluginBase::submitConfigurationForm public function Implements PluginFormInterface::submitConfigurationForm(). Overrides PluginFormInterface::submitConfigurationForm 1
SalesforceMappingFieldPluginBase::validateConfigurationForm public function Implements PluginFormInterface::validateConfigurationForm(). Overrides PluginFormInterface::validateConfigurationForm 1
SalesforceMappingFieldPluginBase::__construct public function SalesforceMappingFieldPluginBase constructor. Overrides PluginBase::__construct 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.