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

add getName method to element types #4211

Merged
merged 4 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion docs/extend/element-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ protected static function defineTableAttributes(): array
```

::: tip
The first attribute you list here is a special case. It defines the header for the first column in the table view, which is the only one admins can’t remove. Its values will be the element string representations (whatever their `__toString()` methods return).
The first attribute you list here is a special case. It defines the header for the first column in the table view, which is the only one admins can’t remove. Its values will come from your elements’ <api:craft\base\ElementInterface::getName()> method.
:::

If it’s a big list, you can also limit which columns should be visible by default for new [sources](#sources) by adding a protected `defineDefaultTableAttributes()` method to your element class:
Expand Down
10 changes: 9 additions & 1 deletion src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -1263,14 +1263,22 @@ public function getLink()
$url = $this->getUrl();

if ($url !== null) {
$link = '<a href="' . $url . '">' . Html::encode($this->__toString()) . '</a>';
$link = '<a href="' . $url . '">' . Html::encode($this->getName()) . '</a>';

return Template::raw($link);
}

return null;
}

/**
* @inheritdoc
*/
public function getName(): string
{
return (string)$this;
}

/**
* @inheritdoc
*/
Expand Down
7 changes: 7 additions & 0 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ public function getUrl();
*/
public function getLink();

/**
* Returns a string to be used in for links to edit this element.
*
* @return string
*/
public function getName(): string;

/**
* Returns the reference string to this element.
*
Expand Down
4 changes: 1 addition & 3 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,7 @@ public function getFullName()
}

/**
* Returns the user's full name or username.
*
* @return string
* @inheritdoc
*/
public function getName(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ private function _getCpElementHtml(array &$context)

$html .= '<span class="title">';

$label = HtmlHelper::encode($element);
$label = HtmlHelper::encode($element->getName());

if ($context['context'] === 'index' && !$element->trashed && ($cpEditUrl = $element->getCpEditUrl())) {
$cpEditUrl = HtmlHelper::encode($cpEditUrl);
Expand Down