Force set Kirby page, pages and site objects by id and language

I’m in a panel field or more correct in a route.

I try to use kirbytext to parse some content, like this:

echo kirbytext( '(image: testimage.jpg)' );

It’s not aware of what page it is, so it prints an image that does not exists.

http://localhost/reveal/testimage.jpg

I tried to force set the page, pages and site objects but no luck:

The get things is get variables.

$page = page( get('id') )->content( get('language') );
$pages = site()->children();
$site = site();

echo kirbytext( '(image: testimage.jpg)' );

It does not make any difference.

Frontend

In a normal template it prints:

http://localhost/reveal/content/some_page/testimage.jpg

It works.

Can I force set the page, pages and site objects to make functions like this work?

Note

It works on the panel home page but not on any other page.

When thinking about it my problem looks more like this:

c::set('routes', array(
  array(
    'pattern' => 'my/awesome/url',
    'action'  => function() {
      $page = page( get('id') )->content( get('language') );
      $pages = site()->children();
      $site = site();

      echo kirbytext( '(image: myimage.jpg)' );
      // Prints wrong url on every page id, except for home.
    }
  )
));

I can’t tell kirbytext on what page I’m at. Or can I?

Why are you trying to do it that way anyway, instead of using the image helper or the brick method?

This is what I’ve been working on:

The images are actually the last piece of the pussle for a first release of my new Reveal plugin. If I’m going to fail on something, it will be the images. :confused:

How it works

  1. I drag an image to the textarea.
  2. An event is triggered.
  3. All the content of the textarea is sent to a route with ajax.
  4. The content is parsed by kirbytext and returned.
  5. The parsed content is put into the preview by a css/js dom selector.

Brick does not seem like an option here. If you have any other thoughts “outside the box”, they are welcome. Thanks! :slight_smile:

You can make Kirbytext aware of the page by constructing a field:

$field = new Field($page, null, '(image: testimage.jpg)');
echo $field->kt();

How about using a pre-filter that pre-parses the image or some sort of regex before you try to parse it via kirbytext?

I tried to parse all the content trough a field. I don’t know how safe that would be but it almost works.

$page = page( get('id') )->content( get('language') );
$content = 'Testing (image: about.jpg) Hehe (image: test.jpg)';
$field = new Field($page, null, $content);
echo $field->kt();

I’m getting something like this:

It fails on rendering the image urls.

Testing
<figure><img src="http://localhost/reveal" alt=""></figure> Hehe
<figure><img src="http://localhost/reveal" alt=""></figure>

Having content set to this also fails:

$content = '(image: about.jpg)';

I could use some pre regexp but I guess the risk is big that it would fail in some cases. I hope to run as much as possible thought Kirbys own functions.

More ideas?

Well, what you pass to the field is not the page but the content object. That’s of course not going to work. What you need is this:

$page = page(get('id'));
$content = 'Testing (image: about.jpg) Hehe (image: test.jpg)';
$field = new Field($page, null, $content);
echo $field->kt();
1 Like

Yes of course! :slight_smile:

It worked and it seems like an awesome solution.

I think the syntax is a little weird tho. Create a new field? In a perfect world I would like it to be like this:

echo kirbytext( $string, $page );

A second argument to kirbytext. But anyway. I’m super happy about this answer. Thanks! :slight_smile:

That’s a nice idea. I have added it to a feature request issue on GitHub.

1 Like

The second argument for kirbytext() is now a feature in Kirby 2.3.2. :slight_smile:

1 Like