/** * Generates a hyperlink that links to the sort action to sort by the specified attribute. * Based on the sort direction, the CSS class of the generated hyperlink will be appended * with "asc" or "desc". * @param string $attribute the attribute name by which the data should be sorted by. * @param array $options additional HTML attributes for the hyperlink tag. * There is one special attribute `label` which will be used as the label of the hyperlink. * If this is not set, the label defined in {@see \rock\data\Sort::$attributes} will be used. * If no label is defined, {@see \rock\helpers\Inflector::camel2words()} will be called to get a label. * Note that it will not be HTML-encoded. * @return string the generated hyperlink * @throws DataProviderException if the attribute is unknown */ public function link($attribute, $options = []) { if (!class_exists('\\rock\\template\\Html')) { throw new DataProviderException(DataProviderException::NOT_INSTALL_TEMPLATE); } if (($direction = $this->getAttributeOrder($attribute)) !== null) { $class = $direction === SORT_DESC ? 'desc' : 'asc'; if (isset($options['class'])) { $options['class'] .= ' ' . $class; } else { $options['class'] = $class; } } $url = $this->createUrl($attribute); $options['data-sort'] = $this->createSortParam($attribute); if (isset($options['label'])) { $label = $options['label']; unset($options['label']); } else { if (isset($this->attributes[$attribute]['label'])) { $label = $this->attributes[$attribute]['label']; } else { $label = Inflector::camel2words($attribute); } } return Html::a($label, $url, $options); }