Using MidCOM template to load schema variables
Traditionally lists for Datamanager widgets select and multiselect are written by hand into a schema. Adding categories to newsticker points a way to do this. Hand-written schemas however don't give the client any convenience to add options later and in these cases it is best to do it dynamically.
MidCOM template is a convenient place to create functions which can be later called in schemas. This is a comfortable way to create a possibility for the client to enter new selection items without having to touch the schema itself or if there is need to cross-link or categorize articles or anything else.
One way is to create a topic either in the site navigation tree or somewhere else. Selection list can then be made of eg. articles or subtopics. This small function is called with GUID to get the first level content.
<?php
// Function to load subtopics or topic articles according to given GUID
function load_guid_content($guid, $type = "article", $sort = "alpha")
{
$parent = mgd_get_object_by_guid($guid);
// For articles
if ($type === "article")
{
$article = mgd_list_topic_articles($parent->id, $sort);
if ($article && $article->N!=0)
{
while ($article->fetch())
{
$return[$article->guid()] = $article->title;
}
}
else
{
$return["null"] = "No {$type}s found";
}
}
// For nodes
elseif ($type === "node")
{
$topic = mgd_list_topics($parent->id, $sort);
if ($topic)
{
while ($topic->fetch())
{
$return[$topic->guid()] = $topic->extra;
}
}
else
{
$return["null"] = "No {$type}s found";
}
}
// Hiding nasty-looking errors from clients
else
{
$return["null"] = "Unspecified type";
}
return $return;
}
?>
Then accordingly to the preferred widget, function can be called in the schema. For the [midcom.helper.datamanager widget select|select]:
"widget_select_choices" => load_guid_content("ff09bde640d0efcf8ab107e98f2233e0", "node"),
and for [midcom.helper.datamanager widget multiselect|multiselect]:
"multiselect_selection_list = load_guid_content("ff09bde640d0efcf8ab107e98f2233e0", "node");
This can be used eg. to boost category options of adding categories to newsticker. In this case, one might want to place field categories between fields title and content to the default de.linkm.newsticker schema and place it in /sitegroup-config/de.linkm.newsticker/schema:
"category" => array (
"description" => "Categories",
"datatype" => "multiselect",
"widget" => "multiselect",
"multiselect_selection_list" => load_guid_content("ff09bde640d0efcf8ab107e98f2233e0", "node"),
),
More detailed description of the process in Adding categories to newsticker.
