October CMS – quick start 17 – adding options to components

Time:2021-9-17

In the previous section, we added a component to the home page through the actor component to display the list of actors.
Next, we will add some properties to the component: define properties
This function will return an array, and each array in the array will return each option

Add an option to display the limit on the number of actors

File:plugins\raiseinfo\movies\components\Actors.php

    public function defineProperties()
    {
        return [
          'results' =>  [
              'title '= >' show the number of actors',
              'description '= >' how many actors do you want to display? ',
              'default' => 0,
              'validationPattern' => '^[0-9]+$',
              'validationmessage' = > 'only numbers are allowed to be entered.'
          ]
        ];
    }

October CMS - quick start 17 - adding options to components

October CMS - quick start 17 - adding options to components

However, we only set the option, which does not control the data. The following solution is to use this option to control the display of data:

File:plugins\raiseinfo\movies\components\Actors.php

    protected function loadActors()
    {
        $query = Actor::all();
        if($this->property('results') > 0){
            $query = $query->take($this->property('results'));
        }
        return $query;
    }

October CMS - quick start 17 - adding options to components

October CMS - quick start 17 - adding options to components

Control actor collation

File:plugins\raiseinfo\movies\components\Actors.php

    public function defineProperties()
    {
        return [
          'results' =>  [
              'title '= >' show the number of actors',
              'description '= >' how many actors do you want to display? ',
              'default' => 0,
              'validationPattern' => '^[0-9]+$',
              'validationmessage' = > 'only numbers are allowed to be entered.'
          ],

          'sortOrder' => [
              'title '= >' actor sort ',
              'description '= >' sort actors',
              'type' => 'dropdown',
              'default' => 'name asc'
          ]

        ];
    }
File:plugins\raiseinfo\movies\components\Actors.php

    public function getSortOrderOptions()
    {
        return [
            'name ASC' = > 'in ascending order of names',
            'name desc' = > 'in descending order of names'
        ];
    }

October CMS - quick start 17 - adding options to components

OK, now the options are displayed. Next, the control of data is realized

File:plugins\raiseinfo\movies\components\Actors.php

protected function loadActors()
    {
        $query = Actor::all();

        if($this->property('sortOrder') == 'name asc'){
            $query = $query->sortBy('name');
        }

        if($this->property('sortOrder') == 'name desc'){
            $query = $query->sortByDesc('name');
        }

        if($this->property('results') > 0){
            $query = $query->take($this->property('results'));
        }
        return $query;
    }

October CMS - quick start 17 - adding options to components

October CMS - quick start 17 - adding options to components

October CMS - quick start 17 - adding options to components

October CMS - quick start 17 - adding options to components

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]