CGCalendar - Dropdown from UDT

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
WDJames
Forum Members
Forum Members
Posts: 12
Joined: Tue Feb 13, 2018 1:09 pm

CGCalendar - Dropdown from UDT

Post by WDJames »

Hi All,

I'm using the CGCalendar module to add course dates. I'm trying to link each course date to its corresponding course page within the site. At the moment, the only way I can do this is by using a custom Text Field and manually adding the page's url. I'd like to use the Dropdown from UDT field so that I can select the page from a dropdown rather than pasting the URL.

Does anyone have any experience in creating a UDT that prints out a list of the site's pages?

Thanks in advance,

James
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3335
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGCalendar - Dropdown from UDT

Post by velden »

What should be the format of the output of the UDT?
That's important for you: do you want to use the page alias or page id the create the links (both are possible).
What do you want the user to see in the dropdown?
WDJames
Forum Members
Forum Members
Posts: 12
Joined: Tue Feb 13, 2018 1:09 pm

Re: CGCalendar - Dropdown from UDT

Post by WDJames »

Hi Velden,

Thanks for the quick reply.

The UDT will be used within the CGCalendar Custom Field (Dropdown from UDT). Unfortunately, I couldn't find any previous examples of what the UDT's format needs to be. I've tried creating a test UDT that prints out a dropdown but it doesn't work as intended so I'm not sure what the output of the UDT should be.

Ideally, when a user creates a new event, there should be a dropdown(Dropdown from UDT) where the user can see a list of the pages title or menu text. Selecting one will output the page's alias. I can then set the template to take this value and use it to link to the page rather than opening the event detail page. Would this be possible?

Thanks,

James
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3335
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGCalendar - Dropdown from UDT

Post by velden »

you could try to create a UDT (for testing) with some simple code:

Code: Select all

return array(
  'alias1' => 'Title 1',
  'alias2' => 'Title 2'
 );
That would tell you if that's the expected output.
WDJames
Forum Members
Forum Members
Posts: 12
Joined: Tue Feb 13, 2018 1:09 pm

Re: CGCalendar - Dropdown from UDT

Post by WDJames »

Hi Velden,

Thanks for the code. That works as expected - the dropdown on the admin side says 'Title 1' and the output on the front end is 'alias1'. All thats left is for the UDT to list the pages.

Thank you so much for your help.

James
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3335
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGCalendar - Dropdown from UDT

Post by velden »

Mostly stolen from the LISE module:

Code: Select all

$contentops = cmsms()->GetContentOperations();
$result = array();
$allcontent = $contentops->GetAllContent(false);

if ($allcontent !== FALSE && count($allcontent) > 0) {

	$result[-1] = 'Choose a page';

	foreach ($allcontent as $one) {

	// Check if object is valid
	if(!is_object($one)) 
	continue;

	// If it doesn't have a valid link...
	// don't include it.
	if(!$one->HasUsableLink())
	continue;
			
	// Don't include content types that do not want children either...
	if (!$one->WantsChildren()) 
		continue;

	// Else append to array.
	$result[$one->Alias()] = $one->MenuText();
	}
}

//print_r($result);
return $result;
WDJames
Forum Members
Forum Members
Posts: 12
Joined: Tue Feb 13, 2018 1:09 pm

Re: CGCalendar - Dropdown from UDT

Post by WDJames »

Hi Velden,

You're a star! Thanks again, the code is exactly what I needed. Maybe a way to extend it in the future is to narrow down the list of pages to the children of a specific page/section header rather than a list of all pages. For now though, this works great! Thanks again for your help, it is very much appreciated.

Thanks,

James
cyrcle
Forum Members
Forum Members
Posts: 23
Joined: Fri Jan 06, 2017 3:54 pm

Re: CGCalendar - Dropdown from UDT

Post by cyrcle »

Hello. For my part, I have a 100% Smarty solution for this.

— #1
In "Site Admin / CGCalendar Settings" add field, Name: "InternalLink", Type: "Dropdown".

— #2
Duplicate the admin_add_event.tpl template from "modules/CGCalendar/templates" in "assets/module_custom/CGCalendar/templates".
You must also create the folders if they do not already exist.

In this new admin_add_event.tpl template you replace:

Code: Select all

    {if isset($fields)}
      {foreach from=$fields item='one'}
      <div class="pageoverflow">
        <p class="pagetext">{$one->name}</p>
        <p class="pageinput">{$one->field}</p>
      </div>
      {/foreach}
    {/if}
by:

Code: Select all

    {if isset($fields)}
      {foreach from=$fields item='one'}
      {if $one->name == 'InternalLink'}{assign var='mavar' value="Page to which this event is linked (optional)"}
      {else}{assign var='mavar' value=$one->name}
      {/if}
      {if $one->name == 'InternalLink'}
      <div class="pageoverflow">
        <p class="pagetext">{$mavar} :</p>
        <p class="pageinput">
        {setlist var=tableau_pages value={cms_module module="Navigator" template="Pages List JSONString" number_of_levels=2 childrenof="projects"}}
        {html_options name="m1_field_InternalLink" options=$tableau_pages selected=$one->value}
        </p>
      </div>
      {else}
      <div class="pageoverflow">
        <p class="pagetext">{$mavar} :</p>
        <p class="pageinput">{$one->field}</p>
      </div>
      {/if}
      {/foreach}
    {/if}
— #3
Then, in the DesignManager, create the "Pages List JSONString" template of type Navigator: Navigation whose content will be:

Code: Select all

{strip}
{* simple navigation / Note : function can only be defined once *}

{function name=Liste_pages depth=1}
{ldelim}
 {foreach $data as $node}{* build the menu item node *}
  {if $node->type == 'sectionheader'}
  {else if $node->type == 'separator'}
  {else}{* regular item *}
   {if $depth==0 && $node@first}{* replace first level by option "-1":"Choose…" *}
    "-1":"Choose…"
   {elseif $depth==0}
    ,"{$node->menutext|escape:"quotes"}":
   {elseif $depth==1}
    "{$node->id}":"{$node->menutext|escape:"quotes"}"{if !$node@last},{/if}{*"\n"*}
   {/if}
   {if isset($node->children)}
    {Liste_pages data=$node->children depth=$depth+1}
   {/if}
  {/if}
 {/foreach}
{rdelim}
{/function}

{if isset($nodes)}
{Liste_pages data=$nodes depth=0}
{/if}
{/strip}
WDJames
Forum Members
Forum Members
Posts: 12
Joined: Tue Feb 13, 2018 1:09 pm

Re: CGCalendar - Dropdown from UDT

Post by WDJames »

Hi Cyrcle,

Thanks for the input, I've tried following your solution but when I go to add an event, I can see the dropdown but it is empty. I know that the template is working because the prompt for the dropdown is "Page to which this event is linked (optional) :" rather than "InternalLink". Was there anymore to the code?

Thanks,

James
adrienj
Forum Members
Forum Members
Posts: 23
Joined: Tue May 13, 2008 1:22 pm

Re: CGCalendar - Dropdown from UDT

Post by adrienj »

Probably, in admin_add_event.tpl, in childrenof = "projects", you must replace "projects" by the name of the alias of the parent section which contains your course pages.

And you may also need to insert some debugging code to follow the value of your variables step by step.
For example, in admin_add_event.tpl after the code you modified. You can add:
{$tableau_pages|@debug_print_var:4:400}
to check that you have a table containing the list of your course pages.
Post Reply

Return to “Modules/Add-Ons”