-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-resource.php
87 lines (69 loc) · 2.88 KB
/
single-resource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* The Template for displaying all single posts
*/
namespace App;
use App\Http\Controllers\Controller;
use App\PostTypes\Resource;
use ImLiam\ShareableLink;
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
use Rareloop\Lumberjack\Post;
use Timber\Timber;
class SingleResourceController extends Controller
{
public function handle()
{
$context = Timber::get_context();
$resource = new Resource();
$context['resource'] = [];
$context['resource']['title'] = $resource->title;
$context['resource']['image_url'] = $resource->thumbnail ? $resource->thumbnail->src : null;
$context['resource']['content'] = $resource->content;
$context['resource']['type'] = $resource->resource_type;
if ($context['resource']['type']) {
$context['resource']['type'] = get_term($context['resource']['type']);
$context['resource']['colour'] = get_field('colour', $context['resource']['type']);
}
$context['resource']['attachments'] = get_field('attachments', $resource->ID);
$context['resource']['link'] = get_field('link', $resource->ID);
if ($resource->organisation) {
$context['resource']['organisation'] = $resource->organisation;
$context['resource']['meta'] = sprintf(__('By %s', 'ocp'), $resource->organisation) . ' / ' . $resource->date('Y');
} else {
$context['resource']['meta'] = $resource->date('Y');
}
$context['resource']['published'] = $resource->date('Y');
// terms
$context['resource']['taxonomies']['issue'] = $resource->issue;
$context['resource']['taxonomies']['region'] = $resource->region;
// for each of the taxonomies, convert the term id to a term object
foreach ($context['resource']['taxonomies'] as $taxonomy => &$terms) {
if (is_array($terms)) {
$terms = array_map(function ($term) {
return get_term($term);
}, $terms);
}
}
$share_links = new ShareableLink($resource->link(), $resource->title);
$context['resource']['share_links'] = array(
'twitter' => $share_links->twitter,
'facebook' => $share_links->facebook,
'linkedin' => $share_links->linkedin
);
$context['resource']['i18n']['download_label'] = _x(
'Download resource',
'The download label for a resource',
'ocp'
);
$context['resource']['i18n']['view_label'] = _x(
'View resource',
'The view label for a resource',
'ocp'
);
$context['back_link'] = [
'url' => get_post_type_archive_link('resource'),
'label' => __('View all resources', 'ocp')
];
return new TimberResponse('templates/single-resource.twig', $context);
}
}