Skip to content

Commit 16e79a8

Browse files
committed
Add index link to Text field
1 parent 2cfc217 commit 16e79a8

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/components/text/IndexField.vue

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77
v-else
88
class="whitespace-no-wrap"
99
:class="applyColor"
10-
>{{ field.value }}</span>
10+
>
11+
<template v-if="!field.showLinkInIndex">
12+
{{ field.value }}
13+
</template>
14+
<router-link
15+
v-else
16+
:class="linkClasses"
17+
:to="{
18+
name: 'detail',
19+
params: {
20+
resourceName: resourceName,
21+
resourceId: field.id
22+
}
23+
}"
24+
:title="field.value">
25+
{{ field.value }}
26+
</router-link>
27+
</span>
1128
</template>
1229

1330
<script>
@@ -16,6 +33,12 @@ import Colors from '../../mixins/Colors';
1633
export default {
1734
mixins: [Colors],
1835
19-
props: ['resourceName', 'field']
36+
props: ['resourceName', 'field'],
37+
38+
computed: {
39+
linkClasses() {
40+
return this.field.indexLinkClasses
41+
}
42+
}
2043
};
2144
</script>

src/Configurable.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace R64\NovaFields;
44

5+
use Config;
6+
57
trait Configurable
68
{
79
/**
@@ -39,6 +41,13 @@ trait Configurable
3941
*/
4042
public $panelLabelClasses = 'w-1/4 py-4';
4143

44+
/**
45+
* The base label classes for the detail view.
46+
*
47+
* @var string
48+
*/
49+
public $indexLinkClasses = 'no-underline dim text-primary font-bold';
50+
4251
/**
4352
* The base excerpt classes.
4453
*
@@ -189,6 +198,19 @@ public function panelLabelClasses($classes)
189198
return $this;
190199
}
191200

201+
/**
202+
* Set the link classes in index view that should be applied instead of the default.
203+
*
204+
* @param string $classes
205+
* @return $this
206+
*/
207+
public function indexLinkClasses($classes)
208+
{
209+
$this->indexLinkClasses = $classes;
210+
211+
return $this;
212+
}
213+
192214
/**
193215
* Add classes to the base label classes for detail view.
194216
*
@@ -264,6 +286,16 @@ public function hideFromDetail()
264286
return $this->withMeta(['hideFromDetail' => true]);
265287
}
266288

289+
/**
290+
* Specify that the index view should show as a link to the resource
291+
*
292+
* @return $this
293+
*/
294+
public function showLinkInIndex()
295+
{
296+
return $this->withMeta(['showLinkInIndex' => true]);
297+
}
298+
267299
/**
268300
* Specify that the element should be shown only on detail view.
269301
*
@@ -372,6 +404,36 @@ public function onlyWhen($field)
372404
return $this->withMeta(['onlyWhen' => $field]);
373405
}
374406

407+
/**
408+
* Overriding the base method in order to grab the model ID.
409+
*
410+
* @param mixed $resource The resource class
411+
* @param string $attribute The attribute of the resource
412+
*
413+
* @return mixed
414+
*/
415+
protected function resolveAttribute($resource, $attribute)
416+
{
417+
$this->setResourceId(data_get($resource, 'id'));
418+
return parent::resolveAttribute($resource, $attribute);
419+
}
420+
/**
421+
* Sets the
422+
*
423+
* @param mixed $id The ID of the resource model. Also sets the base URL based on the Nova config
424+
*
425+
* @return void
426+
*/
427+
protected function setResourceId($id)
428+
{
429+
$path = Config::get('nova.path');
430+
// If the path is the site route, prevent a double-slash
431+
if ('/' === $path) {
432+
$path = '';
433+
}
434+
return $this->withMeta(['id' => $id, 'nova_path' => $path]);
435+
}
436+
375437
/**
376438
* Get additional meta information to merge with the element payload.
377439
*
@@ -386,7 +448,9 @@ public function meta()
386448
'panelFieldClasses' => $this->panelFieldClasses,
387449
'labelClasses' => $this->labelClasses,
388450
'panelLabelClasses' => $this->panelLabelClasses,
451+
'indexLinkClasses' => $this->indexLinkClasses,
389452
'excerptClasses' => $this->excerptClasses,
390453
], $this->meta);
391454
}
455+
392456
}

0 commit comments

Comments
 (0)