eZ\Publish\Core\Search\Common\FieldNameResolver::getSortFieldName PHP Method

getSortFieldName() public method

The method will check for custom fields if given $sortClause implements CustomFieldInterface. With optional parameter $name specific field from field type's Indexable implementation can be targeted. Will return null if no sortable field is found.
See also: eZ\Publish\API\Repository\Values\Content\Query\CustomFieldInterface
See also: eZ\Publish\SPI\FieldType\Indexable
public getSortFieldName ( eZ\Publish\API\Repository\Values\Content\Query\SortClause $sortClause, string $contentTypeIdentifier, string $fieldDefinitionIdentifier, null | string $name = null ) : null | string
$sortClause eZ\Publish\API\Repository\Values\Content\Query\SortClause
$contentTypeIdentifier string
$fieldDefinitionIdentifier string
$name null | string
return null | string
    public function getSortFieldName(SortClause $sortClause, $contentTypeIdentifier, $fieldDefinitionIdentifier, $name = null)
    {
        $fieldMap = $this->getSearchableFieldMap();
        // First check if field exists in type, there is nothing to do if it doesn't
        if (!isset($fieldMap[$contentTypeIdentifier][$fieldDefinitionIdentifier])) {
            return null;
        }
        $fieldName = array_keys($this->getIndexFieldName($sortClause, $contentTypeIdentifier, $fieldDefinitionIdentifier, $fieldMap[$contentTypeIdentifier][$fieldDefinitionIdentifier]['field_type_identifier'], $name, true));
        return reset($fieldName);
    }

Usage Example

 /**
  * Get sort field name
  *
  * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause $sortClause
  * @param string $contentTypeIdentifier
  * @param string $fieldDefinitionIdentifier
  * @param string $name
  *
  * @return array
  */
 protected function getSortFieldName(
     SortClause $sortClause,
     $contentTypeIdentifier,
     $fieldDefinitionIdentifier,
     $name = null
 )
 {
     return $this->fieldNameResolver->getSortFieldName(
         $sortClause,
         $contentTypeIdentifier,
         $fieldDefinitionIdentifier,
         $name
     );
 }