Skip to content

Commit

Permalink
Merge pull request #7067 from bolt/hotfix/title_format
Browse files Browse the repository at this point in the history
Fixes for `title_format`
  • Loading branch information
GwendolenLynch committed Oct 11, 2017
2 parents 33436d4 + a490b4b commit c7d1813
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/view/twig/_macro/_macro.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#}
{% macro contentlink(contenttype, content) %}
{% import _self as crosslinks %}
{% set title = content.getTitle(true) %}
{% set title = content.getTitle() %}
{{ crosslinks.contentlink_by_id(contenttype, title, content.id) }}
{% endmacro %}

Expand Down
16 changes: 12 additions & 4 deletions src/Storage/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,21 @@ public function getValues()
*/
public function getTitle()
{
if (array_key_exists('title', $this->_fields)) {
return $this->_fields['title'];
$fields = $this->_fields;

if (array_key_exists('title', $fields)) {
return $fields['title'];
}

$fieldName = $this->getTitleColumnName($this->contenttype);
$fieldNames = $this->getTitleColumnNames($this->contenttype);

$title = [];
foreach ($fieldNames as $fieldName) {
// Make sure we add strings only, as some fields may be an array or DateTime.
$title[] = is_array($fields[$fieldName]) ? implode(' ', $fields[$fieldName]) : (string) $fields[$fieldName];
}

return $this->$fieldName;
return implode(' ', $title);
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/Storage/Entity/ContentValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ public function getTitle($allowBasicTags = false)
}

foreach ($this->getTitleColumnName() as $fieldName) {
if (strip_tags($this->values[$fieldName], $allowedTags) !== '') {
$titleParts[] = strip_tags($this->values[$fieldName], $allowedTags);
// Make sure we add strings only, as some fields may be an array or DateTime.
$value = is_array($this->values[$fieldName]) ? implode(' ', $this->values[$fieldName]) : (string) $this->values[$fieldName];
if (strip_tags($value, $allowedTags) !== '') {
$titleParts[] = strip_tags($value, $allowedTags);
}
}

Expand All @@ -616,11 +618,7 @@ public function getTitleColumnName()
{
// If we specified a specific fieldname or array of fieldnames as 'title'.
if (!empty($this->contenttype['title_format'])) {
if (!is_array($this->contenttype['title_format'])) {
$this->contenttype['title_format'] = [$this->contenttype['title_format']];
}

return $this->contenttype['title_format'];
return (array) $this->contenttype['title_format'];
}

// Sets the names of some 'common' names for the 'title' column.
Expand Down
56 changes: 37 additions & 19 deletions src/Storage/Mapping/ContentTypeTitleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,57 @@ trait ContentTypeTitleTrait
*
* @param ContentType|array $contentType
*
* @return array
* @return string
*/
protected function getTitleColumnName($contentType)
{
Deprecated::method();

$fields = $contentType['fields'];
$names = $this->getTitleColumnNames($contentType);

return reset($names);
}

/**
* Get an array of the columnname(s) of the title.
*
* @param ContentType|array $contentType
*
* @return array
*/
protected function getTitleColumnNames($contentType)
{
Deprecated::method();

// If we specified a specific fieldname or array of fieldnames as 'title'.
if (!empty($contentType['title_format'])) {
return (array) $contentType['title_format'];
}

$names = [
// EN
'title', 'name', 'caption', 'subject',
// NL
'titel', 'naam', 'onderwerp',
// FR
'nom', 'sujet',
// ES
'nombre', 'sujeto',
// PT
'titulo', 'nome', 'subtitulo', 'assunto',
'title', 'name', 'caption', 'subject', //EN
'titel', 'naam', 'onderwerp', // NL
'nom', 'sujet', // FR
'nombre', 'sujeto', // ES
'titulo', 'nome', 'subtitulo', 'assunto', // PT
];

foreach ($fields as $name => $values) {
if (in_array($name, $names)) {
return $name;
foreach ($names as $name) {
if (isset($contentType['fields'][$name])) {
return [$name];
}
}

foreach ($fields as $name => $values) {
if ($values['type'] === 'text') {
return $name;
// Otherwise, grab the first field of type 'text', and assume that's the title.
if (!empty($contentType['fields'])) {
foreach ($contentType['fields'] as $key => $field) {
if ($field['type'] === 'text') {
return [$key];
}
}
}

// If this is a contenttype without any textfields
// If this is a ContentType without any textfields
$keys = array_keys($fields);

return reset($keys);
Expand Down

0 comments on commit c7d1813

Please sign in to comment.