getkirby / getkirby.com

Source code and content for the Kirby website
https://getkirby.com
132 stars 270 forks source link

Columns in Kirbytext: `Trying to get property of non-object` #178

Closed johannschopplich closed 5 years ago

johannschopplich commented 5 years ago

I singly copied & pasted the code provided in the cookbook. Using it the following error will be thrown: Trying to get property of non-object

Whoops highlights the following line:

$field = new Field($text->field->page, '', trim($column));

Tested with/on:

texnixe commented 5 years ago

I can't reproduce this in Kirby RC6 on my machine.

johannschopplich commented 5 years ago

Thanks. I will retest in the afternoon more thourohyly. It's probably on my side then. Although I thought I had enclosed the test case into its core...

johannschopplich commented 5 years ago

@texnixe I rechecked on a fresh starterkit (Kirby 3.0) and am able to reproduce the same error on both my local machine and a shared hosting environment. Sorry I can't narrow it down further. 🙁

Steps to reprodue:

distantnative commented 5 years ago

Can reproduce it as well. I am looking into it.

texnixe commented 5 years ago

I know what the problem is, there are 2 slashes missing, looks like I had something else in my local copy then what is online.

distantnative commented 5 years ago

That and another issue. Could you both please try:

<?php
Kirby::plugin('kirby/columns', [
  'hooks' => [
    'kirbytags:before' => function ($text) {

      $text = preg_replace_callback('!\(columns(…|\.{3})\)(.*?)\((…|\.{3})columns\)!is', function($matches) use($text) {

        $columns = preg_split('!(\n|\r\n)\+{4}\s+(\n|\r\n)!', $matches[2]);
        $html    = [];

        foreach($columns as $column) {
            $field  = new Field(page(), '', trim($column));
            $html[] = '<div class="' . option('kirby.columns.item', 'column') . '">' . kirbytext($field) . '</div>';
        }

        return '<div class="' . option('kirby.columns.container', 'columns') . '">' . implode($html) . '</div>';

      }, $text);

      return $text;
    }

  ]
]);

Basically adding two backslashes to $columns = preg_split('!(\n|\r\n)\+{4}\s+(\n|\r\n)!', $matches[2]);

And fixing $field = new Field(page(), '', trim($column));

texnixe commented 5 years ago

But the really strange thing is, that the source code is ok and only if you copy from the site, the backslashes get removed.

$columns = preg_split('!(\n|\r\n)\+{4}\s+(\n|\r\n)!', $matches[2]);

In the recipe file on line 67.

distantnative commented 5 years ago

@fabianmichael Sounds like a topic to have you involved.

@texnixe But also the complete code example is different from what gets explained afterwards. There the regex is $columns = preg_split('!\R\+{4}\s+\R!', $matches[2]);

johannschopplich commented 5 years ago

@distantnative Your fixed code works in my setup. Thanks!

texnixe commented 5 years ago