How to get edit collection record _id on call of select-request-options

Hi!,

How to get edit collection record _id on call of select-request-options?
my code is -

Edit URL - http://localhost/itpevents360/admin-cockpit/collections/entry/Events/60587050360500003c0035d9

i have need this id - 60587050360500003c0035d9

//request code is -

{
“name”: “categoryName”,
“type”: “select-request-options”,
“label”: “categoryName”,
“options”: {
“request”: “/category-custom”,
“value”: “categoryName”,
“label”: “categoryName”,
“multiple”: false
}
}

// custom code here
$app->on(‘admin.init’, function() {
$this->bind(’/category-custom’, function(){

          $result=array();
	 
	  $route = explode('/', substr($this['route'],1));
	 
	 if(!empty($route[3]))
	 {
		 $id=$route[3];
	 }else{
		$id=''; 
	 }
	 
	 $categoryData = $this->module('collections')->findOne('Events',['_id' => $id]);
	
		foreach ($categoryData['category'] as $category) {

			$result[]=$category['value'];
			
		}	
	
  return $result;
});

});

Above code is not working … any one help me here…

Please use fenced code blocks.

Do you try to use the SelectRequestOptions addon to fetch the entry you are currently editing to reuse the categories, you typed in a different field?

If so, that addon has no option to apply dynamic values to the request.

And you misunderstood a basic concept. If you call https://my-cockpit-url.tld/category-custom, than $this['route'] == '/category-custom'. You could check against the http referer, but that is unreliable. The cleaner way is to fork the addon, add some javascript to send your current id as a param and than do your checks server side.

Or - if I’m right and you are trying to reuse the categories from the current entry, write a custom field, that reads the current values und use them directly.

Yes , i am trying to reuse the categories from the current entry, write a custom field, that reads the current values and use them directly.

Intro

Again: please update your initial post and use fenced code blocks. You can’t expect anyone to answer your question, if you post some random code, that is hard to read. If people aren’t aware of the SelectRequestOptions addon and how it works, they wouldn’t even be able to guess your initial problem.

Let’s assume, the title of this thread would be “How to reuse values from a field while editing entry?” or “How to connect two fields in entry view?”.

Than, you would have started your initial post with some explanation about your goal to enter categories in the first field, to read them and to reuse them in the second field. Now it’s the time to talk about your try with the SelectRequestOptions addon.

Maybe there was a huge language barrier and you didn’t write about it. So you took some time to paint a sketch with arrows and fields to explain the logic visually.

You also remembered to use fenced code blocks, because I asked you to do so a few days ago in a different thread.

Now I wouldn’t feel like wasting my time.

Answer

OK, now I understand. Unfortunately there is no core field, that matches your conditions. If you want to write a custom field, have a look at UIkit v2, riot.js v3 and the core fields to understant the basic logic.

Than have a look at the SEO field, I wrote a while ago. It has a recursive function to find the root entry, so it works with collections, singletons and also if itself is nested (e. g. inside a set field).

Now you can access parent values of your current entry with

this.$root[this.entryName]['categories']

and use them as input for your custom field.

Side note

Even if your initial try would have worked in general, there was a bug in your php script.

$route = explode('/', substr($this['route'], 1));

if (!empty($route[3])) {
    $id = $route[3];
}

There will never be a $route[3], because you added a limit of 1.

See: PHP: explode - Manual

Sorry again disturbing you.

I am sending you step by step here:

Setting of Events collection

Output getting here All Event all categories

Custom code -

$app->on(‘admin.init’, function() {
$this->bind(‘/category-custom’, function(){

        $result=array();
 
	    $categoryData =$this->module('collections')->find('Events');
	
	foreach ($categoryData as $value) {
			
		foreach ($value['category'] as $category) {
            	
				$result[]=$category['value'];
			
		}	
	}
	
   return $result;
  
});

});

Note * This is working fine for fetch All Events with all Categories
but i need only single event categories which i am editing here not all event

bellow is code to fetch single record from collection in custom function but how to get event $id // this is event _id ? this is my question thank you.

$categoryData = $this->module(‘collections’)->findOne(‘Events’,[‘_id’ => $id]);