Jump to content

Ideas for link/button Fieldtype/Inputtype


Pavle
 Share

Recommended Posts

Hello all,

I am thinking how to make a "simple" button that could be edited by user.

For example in a situation where we have an editable teaser and we want to add a internal link to some other page under it.

The button/link would take two parameters:

1.) Text that is displayed on a button (in different languages!)

2.) Page to which button would link to.

3.) Optional: instead of page object, one could also enter external address.

System admin should also be able to add class name that would be added to rendered element.

Field would then simply be used as: $page->button and it would render <a class='custom-class' href="$page->url">Text (in appropriate language)</a>

I noticed it is sort of similar to this module, but with an option for user to select one of the pages (instead of writing down an ID).

My first thoughts were to create Fieldtype/Inputtype module, but it seems sort of complicated (I don't know how to use neither FieldtypeTextLanguage nor FieldtypePage in module).

So I wonder, how you guys are solving this problem?

Thanks!

Žiga

PS.

I am not sure I added this to the right section. If not, please feel free to move it where it's suppose to be.

Link to comment
Share on other sites

If you want your editors to be able to insert the button in a textarea field, your simplest option might be a Hanna code so your editors could do something like:

[[button label="Click Here" link="http://www.mysite.com/mypage"]]

Otherwise you could have a button created to an internal page by providing them with a Pagefield called "linked_page" and set the input field type to PageListSelect+

Then in your template file use the following code:

echo "<a href='{$page->linked_page->url}' class='button'>Default Button</a>";
  • Like 3
Link to comment
Share on other sites

Hey Adrian,

Thanks for such a fast reply! Your solution would work perfectly if I would have only one language OR if editors would be either linking to external pages or they would remember all page-names, but I fear this approach is (in my case) prone to mistakes/errors.

Using:

Fieldset

TextLanguage

Page

Fieldset_END

 

...would actually do everything that I need, it's just that every time I would want to add a link/button to a template I would need to add all this 4 fields, so I was hoping there is an easy solution for combining different fields into a new Fieldtype.

It would be perfect if there would be something like a module for creating custom Fieldtypes, where one would just create/select needed fields (something like creating a page template, but with the ability to create fields on-the-go) and set output (html) and module would generate appropriate Fieldtype and Inputtype based on that.

Or it would be great if there would be a Fieldtype similar to repeater, just without the ability to repeat...so in the way of selectable Fieldset (fields group)...basically if Fieldset could be added to page template in the similar way than repeater :)

post-2420-0-24685100-1410357136_thumb.jp

Link to comment
Share on other sites

You could simply write a small php script, which includes all those fields to selected templates via the api. Only rearranging would be manual work.

Thanks for this tip. I never thought of that.

Plus, unfortunately, I am not that good with php. Could you maybe explain a bit more, how this could be done? :)

Link to comment
Share on other sites

Sorry - I didn't your original post completely and so missed the multilanguage issues.

Ignoring the option for an external address for the moment, using the Pagefield approach would allow this:

echo "<a href='{$page->linked_page->url}' class='button'>{$page->linked_page->title}</a>";

I don't know much about multilanguage things, but wouldn't that use the title for the currently selected language on the front-end?

Back to the custom external address option - given the multilanguage needs, perhaps the new PageTable field (in the dev version of PW which will be released as stable 2.5 in the next week) could help you out - could you make use of a multilanguage title field and a url field.

Maybe a simpler approach since I think you only want one button per page would be to use a Pagefield with the PageListSelect+ option, but if that is left blank, provide a text url field so they can enter an external link. Then for the button labels you could use a textarea field like this:

english: Link

spanish: Viniculo

etc

Then you could parse the textarea for the appropriate language and display that on the button.

Another possibility might be the Multiplier field (also linked from the PageTable field link above) and use that to add a label for each language.

Lots of options - maybe see if one of them sounds good to you and we can help you implement it.

Link to comment
Share on other sites

@Pavle 

Haven't checked if it works, but it should get you going.

Edit: Please look at Soma's post: https://processwire.com/talk/topic/7544-ideas-for-linkbutton-fieldtypeinputtype/#entry72694

	$templates = array();
	$templates[] = $templates->get("templateName1");
	$templates[] = $templates->get("templateName2");
	$templates[] = $templates->get("templateName3");
	$templates[] = $templates->get("templateName4");

	foreach($templates as $template){
		$template->fields->add("fieldset"); //use the fieldnames
		$template->fields->add("text_language");
		$template->fields->add("page");
		$template->fields->add("fieldset_END");

		$template->save();
	}
Link to comment
Share on other sites

I find it strange that a CMS has no link (or link list) feature. After all it's about links. I also would be many times in need for a repeater style slick link field. 

Thing is it can get really tricky really fast to build such a module especially if you need to be able to either enter url, select a internal page, have attributes like open in new window etc. Then maybe need something to enter linktext (also multilanguage), and then maybe even the urls should be different for each language. 

I solved it with either

- repeater

- just have them make the list in wysywg

- markdown textarea (kinda hard for clients)

If I'm lucky and there's only one link required, I solved it using a text language field, a page reference, and a url field. And handle it on template output.

Maybe we can sponsor a link (list) tool.

Link to comment
Share on other sites

@Pavle 

Haven't checked if it works, but it should get you going.

	$templates = array();
	$templates[] = $templates->get("templateName1");
	$templates[] = $templates->get("templateName2");
	$templates[] = $templates->get("templateName3");
	$templates[] = $templates->get("templateName4");

	foreach($templates as $template){
		$template->fields->add("fieldset"); //use the fieldnames
		$template->fields->add("text_language");
		$template->fields->add("page");
		$template->fields->add("fieldset_END");

		$template->save();
	}

I'm not sure what that should be for solving the problem. :)

Anyway before it gets out of hand. 

$templates is a template system variable, you empty it on first line...

If then you could do such a task like:

$tpls = $templates->find("name=basic-page|sitemap");
foreach($tpls as $t){
    $t->fields->add("myfield");
    ...
}

You can also do this

$t = $templates->get("basic-page");
$t->fields->insertBefore( $fields->myfield1, $t->fields->images );
$t->fields->insertAfter( $fields->myfield2, $t->fields->images );
$t->fields->save();
Link to comment
Share on other sites

Using:

Fieldset

TextLanguage

Page

Fieldset_END

 

...would actually do everything that I need, it's just that every time I would want to add a link/button to a template I would need to add all this 4 fields, so I was hoping there is an easy solution for combining different fields into a new Fieldtype.

My code was an example so do this by the api. The $templates variable was so conveniently named , as said didn't tested it :)

Link to comment
Share on other sites

Sorry, I was away yesterday :)

@ LostKobrakai

Of course! I completely forgot all about $template object and the whole fact that one can also manipulate templates like that :)

@ Soma & Adriam

Yeah, I have only one link per page, so I think that I will go with Soma's solution at the end (which is, I guess, what Adrian proposed as well :)

If I'm lucky and there's only one link required, I solved it using a text language field, a page reference, and a url field. And handle it on template output.

I know this post is sort of complicating about little things, but I agree with Soma...I was expecting it would be more elegant at the end (as most of other stuff in PW are).

I am not really an expert in module building (not even an advance builder :) but I notices some other modules are doing similar stuff. For example PWlink for CKEditor has options to Link to URL, Link to Page, Link to File, Open in New window.

The most interesting example module for "fieldtype building module" would be Repeater, without the repeating part. I checked it yesterday and it seems quite complicated, but as far as I understood the repeater field (that we include in the template) is actually a page (template) with all this other fields. I like the fact that we don't actually see those pages anywhere (like we do with the new PageTable field). I mean...i don't know I would imagine some of my clients would be a bit confused when they would see "link1", "link2" subpages (yeah, I have some interesting clients :)  

Link to comment
Share on other sites

I like the fact that we don't actually see those pages anywhere (like we do with the new PageTable field). I mean...i don't know I would imagine some of my clients would be a bit confused when they would see "link1", "link2" subpages (yeah, I have some interesting clients

I wouldn't blame your clients on this one - whenever I use the PageTable field I always use the "Select a parent for items" and use a hidden parent page that clients won't look in. I think using the default parent is very messy. I think PageTable is much cleaner than repeaters for lots of reasons and now with PageTableExtended, it's even better.

Link to comment
Share on other sites

  • 4 weeks later...

@adrian

A bit late, but yeah, you are absolutely right. I completely forgot about all additional options with page select. At the end, I used multiple fields wrapped in filedset...anyway I realized that I can just duplicate fields from other template, if I need buttons on multiple pages (templates).

Thank you all for your help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...