thedevdojo / voyager-themes

Theme Hook for Voyager
81 stars 23 forks source link

Undefined property $id when access themes option #22

Open henryonsoftware opened 5 years ago

henryonsoftware commented 5 years ago

I got this error when access to themes option: Undefined property: stdClass::$id (View: /home/vagrant/code/test/vendor/tcg/voyager/resources/views/formfields/image.blade.php)

After 1 hour deep dive into Voyager Admin source I found the problem is Voyager Admin updated file image.blade.php at line 5, they want get $dataTypeContent->id But helpers.php missing it at line 17 $dataTypeContent = (object)[$key => $content];.

I tried change it to $dataTypeContent = (object)["id" => 0, $key => $content]; and it worked.

I don't know how to fix it, I just report issue and hope you guys fix it as correct way. Thanks!

Padrio commented 5 years ago

For an update-safe fix you can create a new ServiceProvider which includes a helper.php containing a fixed Version.

// app/Providers/Fixes/VoyagerThemeFixProvider.php

namespace App\Providers\Fixes;

use Illuminate\Support\ServiceProvider;

final class VoyagerThemeFixProvider extends ServiceProvider
{
    public function register()
    {
        include 'helper.php';
    }
}
// app/Providers/Fixes/helper.php

if(!function_exists('theme_field')) {
    function theme_field($type, $key, $title, $content = '', $details = '', $placeholder = '', $required = 0){

        $theme = \VoyagerThemes\Models\Theme::where('active', 1)->first();

        $option_exists = $theme->options->where('key', '=', $key)->first();

        $id = '';
        if(isset($option_exists->value)){
            $content = $option_exists->value;
            $id = $option_exists->id;
        }

        $row = (object)['required' => $required, 'field' => $key, 'type' => $type, 'details' => $details, 'display_name' => $placeholder];
        $dataTypeContent = (object)['id' => $id,$key => $content];
        $label = '<label for="'. $key . '">' . $title . '<span class="how_to">You can reference this value with <code>theme(\'' . $key . '\')</code></span></label>';
        $details = '<input type="hidden" value="' . $details . '" name="' . $key . '_details__theme_field">';
        $type = '<input type="hidden" value="' . $type . '" name="' . $key . '_type__theme_field">';
        return $label . app('voyager')->formField($row, '', $dataTypeContent) . $details . $type . '<hr>';
    }
}

Make sure you register this ServiceProvider before the WaveServiceProvider.

This also fixes the error

henryonsoftware commented 5 years ago

Yes, thanks for your solution. I don't know about WaveServiceProvider, I put the fix provider after VoyagerServiceProvider and it worked.

I created a fix-bug PR but I think this repository don't under maintenance anymore.

rbrunner commented 5 years ago

I got this error when access to themes option: Undefined property: stdClass::$id (View: /home/vagrant/code/test/vendor/tcg/voyager/resources/views/formfields/image.blade.php)

After 1 hour deep dive into Voyager Admin source I found the problem is Voyager Admin updated file image.blade.php at line 5, they want get $dataTypeContent->id But helpers.php missing it at line 17 $dataTypeContent = (object)[$key => $content];.

I tried change it to $dataTypeContent = (object)["id" => 0, $key => $content]; and it worked.

I don't know how to fix it, I just report issue and hope you guys fix it as correct way. Thanks!

Your fix worked for me for my Laravel Wave deployment, thank you very much!

henryonsoftware commented 5 years ago

@rbrunner you can safe-fix follow @Padrio suggestion while waiting for my PR https://github.com/thedevdojo/voyager-themes/pull/25 be approved.