Jump to content

Field type: Select files


Peter Knight
 Share

Recommended Posts

Hi 

Is there a field type in the Core which will allow be to select one (or more) files within the PW file system?

For context...

I want to create two stand-alone PHP files which contain simple HTML as follows

  • Call-to-Action-01.php would be <h2>Call us</h2>
  • Call-to-Action-02.php would be <h2>Email us</h2>

These would be stored within the PW /templates directory (I think).

I then want to use RepeaterMatrix to select one or both and output them into my template where necessary.

I was then looking at the $files->render() method and have the following working

<?php $string = $files->render($Call-to-Action01.php); ?>

So that's a good start but now how to select ?

Thanks ?

 

 

Link to comment
Share on other sites

In the RepeaterMatrix field create two repeater matrix item types: cta1 & cta2. They don't even have to have any fields, i guess.

In the template file that should output the repeater items you can output them like this:

<?php

foreach($page->repeater_matrix_field as $item){

  if ($item->type == "cta1"){
    echo wireRenderFile("Call-to-Action01.php");
  }

  if ($item->type == "cta2"){
    echo wireRenderFile("Call-to-Action02.php");
  }
}

?>

RepeaterMatrix is a pro module, though. Not Core.

  • Like 2
Link to comment
Share on other sites

3 hours ago, Peter Knight said:

Is there a field type in the Core which will allow be to select one (or more) files within the PW file system?

Yes, you can use InputfieldTextTags https://processwire.com/blog/posts/pw-3.0.177/

But it would be a lot easier to use HannaCode or a custom Textformatter module which replaces [file=foo-bar.php] with the content of the file foo-bar.php

  • Like 1
Link to comment
Share on other sites

20 hours ago, aagd said:

In the RepeaterMatrix field create two repeater matrix item types: cta1 & cta2. They don't even have to have any fields, i guess.

In the template file that should output the repeater items you can output them like this:

<?php

foreach($page->repeater_matrix_field as $item){

  if ($item->type == "cta1"){
    echo wireRenderFile("Call-to-Action01.php");
  }

  if ($item->type == "cta2"){
    echo wireRenderFile("Call-to-Action02.php");
  }
}

?>

RepeaterMatrix is a pro module, though. Not Core.

Thanks @aagd. Good idea and certainly something I'm about to explore. I used 2 examples but I'll probably at some stage end up with a lot more. My concern is about scalability here and having to create a field to associate with each file. Ideally I'd love a file select field that I can simply point at a directory.

Link to comment
Share on other sites

18 hours ago, bernhard said:

Yes, you can use InputfieldTextTags https://processwire.com/blog/posts/pw-3.0.177/

But it would be a lot easier to use HannaCode or a custom Textformatter module which replaces [file=foo-bar.php] with the content of the file foo-bar.php

Thanks @bernhard
I've been trying this and much to my surprise, I have it working as follows. Have yet to try it within a Repeater Matrix for the moment.

<?php $myfile = $page->fieldtypeselectfile; ?>
<?php echo wireRenderFile("$myfile");?>

I did find a bug whereby this only seems to scan files within the template folder and not a custom directory but I'll mention that on the main Module thread.

Thanks.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...