This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

info17589
info17589

Hi,

I'm trying to pass two variables to a component (tag, category) from the url. The goal is to detect if the variable value exist and to try to pass it to the component. No matter where in the url the variable is. URL could be like: /records/tag/category or /records/category/tag

I came up to this... is it possible to do it simpler?

use JanVince\SmallRecords\Models\Tag;
use JanVince\SmallRecords\Models\Category;

function in_array_r($item, $array){
  if ($item) {
    return preg_match('/"'.$item.'"/i' , json_encode($array));
  } else {
    return null;
  }
}

function onStart()
{

    $tags = json_decode(Tag::get(), TRUE);
    $categories = json_decode(Category::get(), TRUE);

    if ($this->in_array_r($this->param('tag'), $tags)) {
      $tag = $this->param('tag');
    } elseif ($this->in_array_r($this->param('tag'), $categories)) {
      $category = $this->param('tag');
    }

    if ($this->in_array_r($this->param('category'), $categories)) {
      $category = $this->param('category');
    } elseif ($this->in_array_r($this->param('category'), $tags)) {
      $tag = $this->param('category');
    }

    $tag = (isset($tag)) ? $tag : null;
    $category = (isset($category)) ? $category : null;

    $this->page->components['records']->setProperty('tagSlug', $tag);
    $this->page->components['records']->setProperty('categorySlug', $category);

}

Last updated

1-1 of 1

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.