-
Notifications
You must be signed in to change notification settings - Fork 1
/
contactarticlesimage.php
94 lines (79 loc) · 2.25 KB
/
contactarticlesimage.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
88
89
90
91
92
93
94
<?php
/**
* Contact Articles Image Plugin
*
* @copyright Copyright (C) 2020 Viviana Menzel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Utilities\ArrayHelper;
/**
* Plugin adds intro-image to articles in contact
*
* @since 1.0
*/
class PlgContentContactArticlesImage extends CMSPlugin
{
/**
* Database object
*
* @var JDatabaseDriver
* @since 3.3
*/
protected $db;
/**
* Plugin that retrieves contact information for contact
*
* @param string $context The context of the content being passed to the plugin.
* @param mixed $row An object with a "text" property
* @param mixed $params Additional parameters. See {@see PlgContentContent()}.
* @param integer $page Optional page number. Unused. Defaults to zero.
*
* @return boolean True on success.
*/
public function onContentPrepare($context, &$row, $params, $page = 0)
{
if ($context != 'com_contact.contact' || empty($row->id))
{
return true;
}
$articleids = ArrayHelper::getColumn($row->articles, 'id');
$articleimages = $this->getArticleImage($articleids);
foreach ($row->articles as $article)
{
if (!isset($articleimages[$article->id]))
{
continue;
}
$images = json_decode($articleimages[$article->id]->images);
$introImage = $images->image_intro;
$introImageAlt = $images->image_intro_alt;
$article->introimage = $introImage;
$article->introimagealt = $introImageAlt;
}
return true;
}
/**
* Retrieve intro image from article
*
* @param array $articleids Id of the articles
*
* @return mixed|null|integer
*/
protected function getArticleImage($articleids)
{
$articleids = ArrayHelper::toInteger($articleids);
$articleids = array_filter($articleids);
if (empty($articleids))
{
return array();
}
$query = $this->db->getQuery(true);
$query->select($this->db->quoteName(array('id', 'images')))
->from($this->db->quoteName('#__content'))
->whereIn($this->db->quoteName('id'), $articleids);
$articleimages = $this->db->setQuery($query)->loadObjectList('id');
return $articleimages;
}
}