You are here

class SearchApiFederatedSolrRemap in Search API Federated Solr 7

Same name and namespace in other branches
  1. 7.3 src/SearchApiFederatedSolrRemap.php \SearchApiFederatedSolrRemap
  2. 7.2 src/SearchApiFederatedSolrRemap.php \SearchApiFederatedSolrRemap

Class SearchApiFederatedSolrRemap Provides a Search API index data alteration that remaps property names for indexed items.

Hierarchy

Expanded class hierarchy of SearchApiFederatedSolrRemap

1 string reference to 'SearchApiFederatedSolrRemap'
search_api_federated_solr_search_api_alter_callback_info in ./search_api_federated_solr.module
Implements hook_search_api_alter_callback_info().

File

src/SearchApiFederatedSolrRemap.php, line 11

View source
class SearchApiFederatedSolrRemap extends SearchApiAbstractAlterCallback {

  /**
   * {@inheritdoc}
   */
  public function propertyInfo() {
    if (is_array($this->options['properties'])) {
      return $this->options['properties'];
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function alterItems(array &$items) {
    foreach ($items as &$item) {
      foreach ($this->options['remap'] as $destination => $source) {
        if ($source && isset($item->{$source})) {
          $item->{$destination} = $item->{$source};
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function configurationForm() {
    $form['remap'] = [
      '#type' => 'fieldset',
      '#title' => t('Remap properties'),
    ];
    foreach ($this
      ->federatedFields() as $k => $field) {
      $form['remap'][$k] = [
        '#type' => 'select',
        '#title' => $field['name'],
        '#options' => $this
          ->indexFieldOptions(),
        '#default_value' => isset($this->options['remap'][$k]) ? $this->options['remap'][$k] : '',
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   *
   * Not all of the source field information is available when the propertyInfo()
   * method is called, so we set up the properties here and store them in the
   * plugin options.
   */
  public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
    $properties = [];
    $fields = $this->index
      ->getFields(FALSE);
    foreach (array_filter($values['remap']) as $destination => $source) {
      $properties[$destination] = [
        'label' => t('@field (remapped from @key)', [
          '@field' => $fields[$source]['name'],
          '@key' => $source,
        ]),
        'description' => $fields[$source]['description'],
        'type' => $fields[$source]['type'],
      ];
    }
    $values['properties'] = $properties;
    return parent::configurationFormSubmit($form, $values, $form_state);

    // TODO: Change the autogenerated stub
  }

  /**
   * This is the list of possible destination fields.
   *
   * @return array
   *
   * @see docs/federated_schema.md
   */
  protected function federatedFields() {
    return [
      'federated_title' => [
        'name' => t('Federated Title'),
        'description' => '',
        'type' => 'string',
      ],
      'federated_date' => [
        'name' => t('Federated Date'),
        'description' => '',
        'type' => 'date',
      ],
      'federated_type' => [
        'name' => t('Federated Type'),
        'description' => '',
        'type' => 'string',
      ],
      'federated_terms' => [
        'name' => t('Federated Terms'),
        'description' => '',
        'type' => 'string',
      ],
      'federated_image' => [
        'name' => t('Federated Image'),
        'description' => '',
        'type' => 'uri',
      ],
      'rendered_item' => [
        'name' => t('Rendered Item'),
        'description' => '',
        'type' => 'text',
      ],
    ];
  }

  /**
   * Get a form options array containing all of the fields that can be remapped.
   *
   * @return array
   */
  protected function indexFieldOptions() {
    $options = array_diff_key($this->index
      ->getFields(FALSE), $this
      ->federatedFields());
    array_walk($options, function (&$item, $key) {
      $item = t('@name (@machine_name)', [
        '@name' => $item['name'],
        '@machine_name' => $key,
      ]);
    });
    return [
      '- ' . t('None') . ' -',
    ] + $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiAbstractAlterCallback::$index protected property The index whose items will be altered.
SearchApiAbstractAlterCallback::$options protected property The configuration options for this callback, if it has any.
SearchApiAbstractAlterCallback::configurationFormValidate public function Implements SearchApiAlterCallbackInterface::configurationFormValidate(). Overrides SearchApiAlterCallbackInterface::configurationFormValidate 1
SearchApiAbstractAlterCallback::isMultiEntityIndex protected function Determines whether the given index contains multiple types of entities.
SearchApiAbstractAlterCallback::supportsIndex public function Implements SearchApiAlterCallbackInterface::supportsIndex(). Overrides SearchApiAlterCallbackInterface::supportsIndex 10
SearchApiAbstractAlterCallback::__construct public function Implements SearchApiAlterCallbackInterface::__construct(). Overrides SearchApiAlterCallbackInterface::__construct 1
SearchApiFederatedSolrRemap::alterItems public function Alter items before indexing. Overrides SearchApiAlterCallbackInterface::alterItems
SearchApiFederatedSolrRemap::configurationForm public function Implements SearchApiAlterCallbackInterface::configurationForm(). Overrides SearchApiAbstractAlterCallback::configurationForm
SearchApiFederatedSolrRemap::configurationFormSubmit public function Not all of the source field information is available when the propertyInfo() method is called, so we set up the properties here and store them in the plugin options. Overrides SearchApiAbstractAlterCallback::configurationFormSubmit
SearchApiFederatedSolrRemap::federatedFields protected function This is the list of possible destination fields.
SearchApiFederatedSolrRemap::indexFieldOptions protected function Get a form options array containing all of the fields that can be remapped.
SearchApiFederatedSolrRemap::propertyInfo public function Implements SearchApiAlterCallbackInterface::propertyInfo(). Overrides SearchApiAbstractAlterCallback::propertyInfo