Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some suggested improvements for this module #9

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed use of "Item language" field in Views.
  • Loading branch information
drunken-monkey committed May 25, 2017
commit 8405307dda007373ba815a10ca2d816f4ef089df
12 changes: 9 additions & 3 deletions src/Plugin/DataType/SolrField.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
class SolrField extends TypedData implements \IteratorAggregate, TypedDataInterface {

/**
* The wrapped Search API Field.
* The wrapped Search API field.
*
* @var \Drupal\search_api\Item\FieldInterface|null
*/
@@ -56,7 +56,7 @@ public static function createFromField(FieldInterface $field, $name, TypedDataIn
* {@inheritdoc}
*/
public function getValue() {
return $this->field;
return $this->field ? $this->field->getValues() : NULL;
}

/**
@@ -74,7 +74,13 @@ public function setValue($field, $notify = TRUE) {
* {@inheritdoc}
*/
public function getIterator() {
return isset($this->field) ? $this->field->getIterator() : new \ArrayIterator([]);
if ($this->field instanceof \Iterator) {
return $this->field;
}
if ($this->field instanceof \IteratorAggregate) {
return $this->field->getIterator();
}
return new \ArrayIterator([]);
}

}
35 changes: 26 additions & 9 deletions src/Plugin/search_api/datasource/SolrDocument.php
Original file line number Diff line number Diff line change
@@ -102,25 +102,22 @@ public function getSolrFieldManager() {
* {@inheritdoc}
*/
public function getItemId(ComplexDataInterface $item) {
return $item->get($this->configuration['id_field'])->getValue();
return $this->getFieldValue($item, 'id_field');
}

/**
* {@inheritdoc}
*/
public function getItemLabel(ComplexDataInterface $item) {
if ($this->configuration['label_field']) {
return $item->get($this->configuration['label_field'])->getValue();
}
return NULL;
return $this->getFieldValue($item, 'label_field');
}

/**
* {@inheritdoc}
*/
public function getItemLanguage(ComplexDataInterface $item) {
if ($this->configuration['language_field']) {
return $item->get($this->configuration['language_field'])->getValue();
return $this->getFieldValue($item, 'language_field');
}
return parent::getItemLanguage($item);
}
@@ -129,10 +126,30 @@ public function getItemLanguage(ComplexDataInterface $item) {
* {@inheritdoc}
*/
public function getItemUrl(ComplexDataInterface $item) {
if ($this->configuration['url_field']) {
return $item->get($this->configuration['url_field'])->getValue();
return $this->getFieldValue($item, 'url_field');
}

/**
* Retrieves a scalar field value from a result item.
*
* @param \Drupal\Core\TypedData\ComplexDataInterface $item
* The result item.
* @param string $config_key
* The key in the configuration.
*
* @return mixed|null
* The scalar value of the specified field (first value for multi-valued
* fields), if it exists; NULL otherwise.
*/
protected function getFieldValue(ComplexDataInterface $item, $config_key) {
if (empty($this->configuration[$config_key])) {
return NULL;
}
$values = $item->get($this->configuration[$config_key])->getValue();
if (is_array($values)) {
$values = $values ? reset($values) : NULL;
}
return NULL;
return $values ?: NULL;
}

/**