Skip to content

Commit

Permalink
[Core] Always add custom meta fields to the templates (see #717).
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed May 9, 2017
1 parent 569d7ad commit 147ce12
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 128 deletions.
1 change: 1 addition & 0 deletions core-bundle/CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### 4.4.0-beta1 (2017-05-05)

* Always add custom meta fields to the templates (see #717).
* Warn if another user has edited a record when saving it (see #809).
* Optimize the element preview height (see #810).
* Use the file meta data by default when adding an image (see #807).
Expand Down
73 changes: 13 additions & 60 deletions core-bundle/src/Resources/contao/elements/ContentGallery.php
Expand Up @@ -79,9 +79,6 @@ public function generate()
*/
protected function compile()
{
/** @var PageModel $objPage */
global $objPage;

$images = array();
$auxDate = array();
$objFiles = $this->objFiles;
Expand All @@ -105,37 +102,15 @@ protected function compile()
continue;
}

$arrMeta = $this->getMetaData($objFiles->meta, $objPage->language);

if (empty($arrMeta))
{
if ($this->metaIgnore)
{
continue;
}
elseif ($objPage->rootFallbackLanguage !== null)
{
$arrMeta = $this->getMetaData($objFiles->meta, $objPage->rootFallbackLanguage);
}
}

// Use the file name as title if none is given
if ($arrMeta['title'] == '')
{
$arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
}

// Add the image
$images[$objFiles->path] = array
(
'id' => $objFiles->id,
'uuid' => $objFiles->uuid,
'name' => $objFile->basename,
'singleSRC' => $objFiles->path,
'title' => \StringUtil::specialchars($arrMeta['title']),
'alt' => \StringUtil::specialchars($arrMeta['alt']),
'imageUrl' => $arrMeta['link'],
'caption' => $arrMeta['caption']
'id' => $objFiles->id,
'uuid' => $objFiles->uuid,
'name' => $objFile->basename,
'singleSRC' => $objFiles->path,
'title' => \StringUtil::specialchars($objFile->basename),
'filesModel' => $objFiles->current()
);

$auxDate[] = $objFile->mtime;
Expand Down Expand Up @@ -166,37 +141,15 @@ protected function compile()
continue;
}

$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->language);

if (empty($arrMeta))
{
if ($this->metaIgnore)
{
continue;
}
elseif ($objPage->rootFallbackLanguage !== null)
{
$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->rootFallbackLanguage);
}
}

// Use the file name as title if none is given
if ($arrMeta['title'] == '')
{
$arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
}

// Add the image
$images[$objSubfiles->path] = array
(
'id' => $objSubfiles->id,
'uuid' => $objSubfiles->uuid,
'name' => $objFile->basename,
'singleSRC' => $objSubfiles->path,
'title' => \StringUtil::specialchars($arrMeta['title']),
'alt' => \StringUtil::specialchars($arrMeta['alt']),
'imageUrl' => $arrMeta['link'],
'caption' => $arrMeta['caption']
'id' => $objSubfiles->id,
'uuid' => $objSubfiles->uuid,
'name' => $objFile->basename,
'singleSRC' => $objSubfiles->path,
'title' => \StringUtil::specialchars($objFile->basename),
'filesModel' => $objSubfiles->current()
);

$auxDate[] = $objFile->mtime;
Expand Down Expand Up @@ -355,7 +308,7 @@ protected function compile()
$images[($i+$j)]['imagemargin'] = $this->imagemargin;
$images[($i+$j)]['fullsize'] = $this->fullsize;

$this->addImageToTemplate($objCell, $images[($i+$j)], $intMaxWidth, $strLightboxId);
$this->addImageToTemplate($objCell, $images[($i+$j)], $intMaxWidth, $strLightboxId, $images[($i+$j)]['filesModel']);

// Add column width and class
$objCell->colWidth = $colwidth . '%';
Expand Down
13 changes: 5 additions & 8 deletions core-bundle/src/Resources/contao/elements/ContentHyperlink.php
Expand Up @@ -42,11 +42,6 @@ protected function compile()

$embed = explode('%s', $this->embed);

if ($this->linkTitle == '')
{
$this->linkTitle = $this->url;
}

// Use an image instead of the title
if ($this->useImage && $this->singleSRC != '')
{
Expand All @@ -56,9 +51,7 @@ protected function compile()
{
$this->singleSRC = $objModel->path;
$this->addImageToTemplate($this->Template, $this->arrData, null, null, $objModel);

$this->Template->useImage = true;
$this->Template->linkTitle = \StringUtil::specialchars($this->linkTitle);
}
}

Expand All @@ -78,9 +71,13 @@ protected function compile()
$this->Template->embed_pre = $embed[0];
$this->Template->embed_post = $embed[1];
$this->Template->link = $this->linkTitle;
$this->Template->linkTitle = \StringUtil::specialchars($this->titleText ?: $this->linkTitle);
$this->Template->target = '';

if ($this->titleText)
{
$this->Template->linkTitle = \StringUtil::specialchars($this->titleText);
}

// Override the link target
if ($this->target)
{
Expand Down
76 changes: 50 additions & 26 deletions core-bundle/src/Resources/contao/library/Contao/Controller.php
Expand Up @@ -1553,12 +1553,23 @@ public static function addImageToTemplate($objTemplate, $arrItem, $intMaxWidth=n
$objTemplate->imgSize = ' width="' . $imgSize[0] . '" height="' . $imgSize[1] . '"';
}

// Load the file meta data
if (!$arrItem['overwriteMeta'])
$arrMeta = array();

// Load the meta data
if ($objModel instanceof FilesModel)
{
$arrMeta = array();
if (TL_MODE == 'FE')
{
global $objPage;

$arrMeta = \Frontend::getMetaData($objModel->meta, $objPage->language);

if ($objModel instanceof FilesModel)
if (empty($arrMeta) && $objPage->rootFallbackLanguage !== null)
{
$arrMeta = \Frontend::getMetaData($objModel->meta, $objPage->rootFallbackLanguage);
}
}
else
{
$arrMeta = \Frontend::getMetaData($objModel->meta, $GLOBALS['TL_LANGUAGE']);
}
Expand All @@ -1574,32 +1585,42 @@ public static function addImageToTemplate($objTemplate, $arrItem, $intMaxWidth=n
}
}

// Overwrite the item array
foreach ($arrMeta as $k=>$v)
$arrMeta['imageTitle'] = $arrMeta['title'];
$arrMeta['imageUrl'] = $arrMeta['link'];
unset($arrMeta['title'], $arrMeta['link']);

// Add the meta data to the item
if (!$arrItem['overwriteMeta'])
{
switch ($k)
foreach ($arrMeta as $k=>$v)
{
case 'link':
$arrItem['imageUrl'] = $v;
break;

case 'title':
$arrItem['imageTitle'] = $v;
break;

default:
$arrItem[$k] = $v;
break;
switch ($k)
{
case 'alt':
case 'imageTitle':
$arrItem[$k] = \StringUtil::specialchars($v);
break;

default:
$arrItem[$k] = $v;
break;
}
}
}
}

$picture['alt'] = \StringUtil::specialchars($arrItem['alt']);

// Only add the title if the image is not part of an image link
if (empty($arrItem['imageUrl']) && empty($arrItem['fullsize']))
// Move the title to the link tag so it is shown in the lightbox
if ($arrItem['fullsize'] && $arrItem['imageTitle'] && !$arrItem['linkTitle'])
{
$arrItem['linkTitle'] = $arrItem['imageTitle'];
unset($arrItem['imageTitle']);
}

if (isset($arrItem['imageTitle']))
{
$picture['title'] = \StringUtil::specialchars($arrItem['title']);
$picture['title'] = \StringUtil::specialchars($arrItem['imageTitle']);
}

$objTemplate->picture = $picture;
Expand Down Expand Up @@ -1652,16 +1673,19 @@ public static function addImageToTemplate($objTemplate, $arrItem, $intMaxWidth=n
$objTemplate->attributes = ' data-lightbox="' . substr($strLightboxId, 9, -1) . '"';
}

// Add the meta data to the template
foreach (array_keys($arrMeta) as $k)
{
$objTemplate->$k = $arrItem[$k];
}

// Do not urlEncode() here because getImage() already does (see #3817)
$objTemplate->src = TL_FILES_URL . $src;
$objTemplate->alt = \StringUtil::specialchars($arrItem['alt']);
$objTemplate->title = \StringUtil::specialchars($arrItem['title']);
$objTemplate->linkTitle = \StringUtil::specialchars($arrItem['linkTitle'] ?: $arrItem['title']);
$objTemplate->singleSRC = $arrItem['singleSRC'];
$objTemplate->linkTitle = $arrItem['linkTitle'] ?: $arrItem['title'];
$objTemplate->fullsize = $arrItem['fullsize'] ? true : false;
$objTemplate->addBefore = ($arrItem['floating'] != 'below');
$objTemplate->margin = static::generateMargin($arrMargin);
$objTemplate->caption = $arrItem['caption'];
$objTemplate->singleSRC = $arrItem['singleSRC'];
$objTemplate->addImage = true;
}

Expand Down
45 changes: 11 additions & 34 deletions core-bundle/src/Resources/contao/modules/ModuleRandomImage.php
Expand Up @@ -62,9 +62,6 @@ public function generate()
*/
protected function compile()
{
/** @var PageModel $objPage */
global $objPage;

$images = array();
$objFiles = $this->objFiles;

Expand All @@ -87,24 +84,14 @@ protected function compile()
continue;
}

$arrMeta = $this->getMetaData($objFiles->meta, $objPage->language);

// Use the file name as title if none is given
if ($arrMeta['title'] == '')
{
$arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
}

// Add the image
$images[$objFiles->path] = array
(
'id' => $objFiles->id,
'name' => $objFile->basename,
'singleSRC' => $objFiles->path,
'title' => \StringUtil::specialchars($arrMeta['title']),
'alt' => \StringUtil::specialchars($arrMeta['alt']),
'imageUrl' => $arrMeta['link'],
'caption' => $arrMeta['caption']
'id' => $objFiles->id,
'name' => $objFile->basename,
'singleSRC' => $objFiles->path,
'title' => \StringUtil::specialchars($objFile->basename),
'filesModel' => $objFiles->current()
);
}

Expand Down Expand Up @@ -133,24 +120,14 @@ protected function compile()
continue;
}

$arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->language);

// Use the file name as title if none is given
if ($arrMeta['title'] == '')
{
$arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
}

// Add the image
$images[$objSubfiles->path] = array
(
'id' => $objSubfiles->id,
'name' => $objFile->basename,
'singleSRC' => $objSubfiles->path,
'title' => \StringUtil::specialchars($arrMeta['title']),
'alt' => \StringUtil::specialchars($arrMeta['alt']),
'imageUrl' => $arrMeta['link'],
'caption' => $arrMeta['caption']
'id' => $objSubfiles->id,
'name' => $objFile->basename,
'singleSRC' => $objSubfiles->path,
'title' => \StringUtil::specialchars($objFile->basename),
'filesModel' => $objSubfiles->current()
);
}
}
Expand Down Expand Up @@ -179,6 +156,6 @@ protected function compile()
$arrImage['caption'] = $arrImage['title'];
}

$this->addImageToTemplate($this->Template, $arrImage);
$this->addImageToTemplate($this->Template, $arrImage, null, null, $arrImage['filesModel']);
}
}

0 comments on commit 147ce12

Please sign in to comment.