Commit 0924df8b authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Automatically generate block titles when they are rendered



The blocktype's get_instance_title function was previously called when
blocks were saved.  By moving the call to happen when the block is rendered,
it's possible to (for example) automatically update the title of a blogpost
block by changing the post title.

This change will have no effect on existing blocks, because
get_instance_title is only called when the title stored in the db is empty.

Change-Id: I599108e7b96c80475600d21c3b2054096ce162c0
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent bfbd1fc6
...@@ -520,13 +520,6 @@ class BlockInstance { ...@@ -520,13 +520,6 @@ class BlockInstance {
$title = (isset($values['title'])) ? $values['title'] : ''; $title = (isset($values['title'])) ? $values['title'] : '';
unset($values['title']); unset($values['title']);
$this->set('configdata', $values); $this->set('configdata', $values);
$blocktypeclass = generate_class_name('blocktype', $this->get('blocktype'));
if (!$title && $title !== '0' && method_exists($blocktypeclass, 'get_instance_title')) {
// Get the default title for the block if one isn't set
$title = call_static_method($blocktypeclass, 'get_instance_title', $this);
}
$this->set('title', $title); $this->set('title', $title);
try { try {
...@@ -558,6 +551,20 @@ class BlockInstance { ...@@ -558,6 +551,20 @@ class BlockInstance {
$form->reply(PIEFORM_OK, $result); $form->reply(PIEFORM_OK, $result);
} }
public function get_title() {
$blocktypeclass = generate_class_name('blocktype', $this->get('blocktype'));
if ($override = call_static_method($blocktypeclass, 'override_instance_title', $this)) {
return $override;
}
if ($title = $this->get('title') and $title != '') {
return $title;
}
if (method_exists($blocktypeclass, 'get_instance_title')) {
return call_static_method($blocktypeclass, 'get_instance_title', $this);
}
return '';
}
/** /**
* Builds the HTML for the block, inserting the blocktype content at the * Builds the HTML for the block, inserting the blocktype content at the
* appropriate place * appropriate place
...@@ -642,8 +649,7 @@ class BlockInstance { ...@@ -642,8 +649,7 @@ class BlockInstance {
$smarty->assign('id', $this->get('id')); $smarty->assign('id', $this->get('id'));
$smarty->assign('viewid', $this->get('view')); $smarty->assign('viewid', $this->get('view'));
$title = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'override_instance_title', $this); $smarty->assign('title', $this->get_title());
$smarty->assign('title', $title ? $title : $this->get('title'));
$smarty->assign('column', $this->get('column')); $smarty->assign('column', $this->get('column'));
$smarty->assign('order', $this->get('order')); $smarty->assign('order', $this->get('order'));
...@@ -681,13 +687,12 @@ class BlockInstance { ...@@ -681,13 +687,12 @@ class BlockInstance {
$smarty = smarty_core(); $smarty = smarty_core();
$smarty->assign('id', $this->get('id')); $smarty->assign('id', $this->get('id'));
$smarty->assign('blocktype', $this->get('blocktype')); $smarty->assign('blocktype', $this->get('blocktype'));
$title = call_static_method($classname, 'override_instance_title', $this);
// hide the title if required and no content is present // hide the title if required and no content is present
if (call_static_method($classname, 'hide_title_on_empty_content') if (call_static_method($classname, 'hide_title_on_empty_content')
&& !trim($content)) { && !trim($content)) {
return; return;
} }
$smarty->assign('title', $title ? $title : $this->get('title')); $smarty->assign('title', $this->get_title());
// If this block is for just one artefact, we set the title of the // If this block is for just one artefact, we set the title of the
// block to be a link to view more information about that artefact // block to be a link to view more information about that artefact
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment