0

I'm sure this is very simple but it's proving just a bit beyond me at the moment.

I have made a plugin that I would like to use for displaying galleries which is working fine. However, trying to add the options of the galleries that I have created in my component is proving to be difficult.

When I add the component to a page, I have now got the option to choose all the galleries that I created but displaying the gallery based upon which one I selected is what I have been unsuccessful in doing.

Any help would be greatly appreciated!

I'm sure this is very simple but it's proving just a bit beyond me at the moment.

I have made a plugin that I would like to use for displaying galleries which is working fine. However, trying to add the options of the galleries that I have created in my component is proving to be difficult.

When I add the component to a page, I have now got the option to choose all the galleries that I created but displaying the gallery based upon which one I selected is what I have been unsuccessful in doing.

Any help would be greatly appreciated!

Components/Gallery.php:

use Cms\Classes\ComponentBase;
use MartinSmith\Gallerys\Models\Gallery as GalleryModel;

class gallerys extends ComponentBase
{
public $gallery;

public function componentDetails(){
    return [
        'name' => 'Frontend Gallery',
        'description' => 'A gallery for you webpage'
    ];
}

public function defineProperties() {
    $lists = $this->getLists();
    return [
        'galleryName' => [
            'title' => 'Gallery',
            'type' => 'dropdown',
            'placeholder' => 'Select Gallery',
            'options' => $lists
        ]
    ];
}

public function getLists() {
    $agreements = GalleryModel::all()->pluck('name', 'id');
    return $agreements->toArray();
}

public function getList() {
    $agreement = GalleryModel::where('id', $this->property('galleryName'))->get();
    return $agreement->first();
}

}

Components/gallery/default.htm:

{% set gallerys = __SELF__.gallery %}

{% for gallery in gallerys %}

<div class="container-fluid px-0">

    <div class="gallery">

        <div class="row">

            {% for image in gallery.fullImage %}

            <div class="col-md-4 px-0 home-galleryImg">

                <a href="{{ image.path }}">

                    <div class="gallery-imgOverlay">
                        <p>{{ image.title }}</p>
                        <h5>{{ image.description }}</h5>
                    </div>

                    <img class="img-fluid" src="{{ image.thumb(650,auto) }}" alt="{{ thumbnail.description }}">

                </a>

            </div>

            {% endfor %}

        </div>

    </div>

</div>

{% endfor %}

See screenshot

1 Answers1

0

I solved this for myself by creating a function that returns the "name" and indexed by the 'id' using the laravel pluck method. pluck('name', 'id') The first argument selects the column to use as the value and the second argument selects the column to use as a key. Note* the toArray() method I don't think the options field can take collections.

public function getLists() {
    $agreements = Agreements::all()->pluck('agrnum', 'id');
    return $agreements->toArray();
}
//returns
array:3 [▼
  2 => "DLE-2"
  4 => "DLE-1"
  5 => "DLE-3"
]

Now in my properties area I call the function $list = $this->getList();

public function defineProperties() {
$lists = $this->getLists();
return [
    'getList' => [
        'title' => 'List',
        'type' => 'dropdown',
        'placeholder' => 'Select List',
        'options'     => $lists
       ]
    ];
}

After that you can proceed to do a Lists::where('id', $this->property('getList')); or something of that sort in a function to show the selected list or in your case gallery.

My results: The CMS Page Backend from component

    public function defineProperties() {
    $lists = $this->getLists();
    return [
        'getList' => [
            'title' => 'List',
            'type' => 'dropdown',
            'placeholder' => 'Select List',
            'options'     => $lists
           ]
        ];
    }

    public function getLists() {
    $agreements = Agreements::all()->pluck('agrnum', 'id');
    return $agreements->toArray();
    }

    public function getList() {
        $agreement = Agreements::where('id', $this->property('getList'))->get();
        return $agreement->first();
    }

enter image description here

The Webpage from default.htm in the component template folder

{{ d(__SELF__.getList) }}

enter image description here

Also if I do {{ d(__SELF__.property('getList')) }} it shows me the value is "5".

Pettis Brandon
  • 875
  • 1
  • 6
  • 8
  • Thank you for the advice. I have tried what you have suggested but I end up with the following error: Object of class October\Rain\Database\Builder could not be converted to string. Any ideas? – MartinJuniorS Apr 23 '19 at 22:06
  • @MartinJuniorS Could you post the amended code with what I suggested. Where are you getting the error? Viewing the CMS Page in the backend or the webpage itself in the front end? I amended my answer to show you more of what mine looks like. In your Component.php don't be afraid to trace things with dd(); . The die and dump ( dd() ) can be useful to trace where the php execution error occurs. – Pettis Brandon Apr 24 '19 at 00:01
  • Ok, I have updated the code and you should see that it is somewhat identical to the code in which you sent over. I left the variables and the function names the same to avoid confusion. As you can see from the screenshot, +attributes does show the correct gallery now and when I change the component to say 'test 2', it does also update but the galleries are not being outputted. – MartinJuniorS Apr 24 '19 at 07:55
  • Oh and also, when I do {{ d(__SELF__.property('galleryName')) }}, I get a value of 18 – MartinJuniorS Apr 25 '19 at 07:27
  • If you want to get a specific gallery and specifying it with the component change `{% set gallerys = __SELF__.gallery %}` to `{% set gallerys = __SELF__.getList %}` in your component default.htm. – Pettis Brandon Apr 25 '19 at 08:18
  • Sorry, I realise this is certainly user error. For '{% set gallerys = __SELF__.getList %}', the .getList, is this reference to the function or the property value? It also breaks when I use either .galleryName or .getList as I thought my original .gallery was in reference to the model and not the component. Sorry, I'm making this difficult but the reason for this was to develop a better understanding and being able to expand on websites by building plugins that allowed me to extend functionality of a website. Thanks for all your help so far – MartinJuniorS Apr 25 '19 at 13:35
  • Okay after looking at this again I see what happened. In your `public function getList()` remove the `->first()`. The `where()` method returns a collection because it plans for multiple items to mach the comparison. The `first()` method then takes the first item in that collection. In your default.htm you are using `{% for %}` which expects `{{ __SELF__.getList }}` to be an array. [The documents](https://octobercms.com/docs/plugin/components#referencing-self) Says that .getList is a component property. – Pettis Brandon Apr 25 '19 at 15:14
  • Woo! Yes, that did indeed fix the issue. I'm getting there, just slowly! Thanks for you help – MartinJuniorS Apr 25 '19 at 16:16
  • Hi there, thanks for all your help in the past! Just wondering if you might be able to give me a hand with this: https://stackoverflow.com/questions/57020448/how-to-return-the-key-value-from-a-checkbox-list-field-which-automatically-updat - Sorry to be so cheeky! – MartinJuniorS Jul 13 '19 at 18:27