CB field code dropdown single select

1 month 1 week ago #337564 by activha
CB field code dropdown single select was created by activha
Hello

I am importing a php code in a cb field code dropdown single select.

Could you tell me what is needed in the value/label of the global tab in order to get the data to be displayed ?

My code gets an array like this :
<?php

use Joomla\CMS\Factory;
use Joomla\CMS\Http\HttpFactory;
use Joomla\CMS\Uri\Uri;

if (!file_exists(JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php') || 
    !file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php')) {
    echo 'CB not installed';
    return;
}

$userid = '[user_id]';
$token = '[cb_token_api]';

$ndata = [
    'userid' => $userid,  
    'command' => 'getNdbModelsCommand'
];

$url = Uri::base() . 'devhandle-command';
$http = HttpFactory::getHttp();
$options = [
    'headers' => [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $token
    ]
];
$response = $http->post($url, json_encode($ndata), $options);
$data = json_decode($response->body, true);

if (isset($data['data']) && is_array($data['data'])) {
    $models = [];
    foreach ($data['data'] as $model) {
        $models[] = $model; 
    }
    var_dump($models);
} else {
    echo 'No data received or error';
    var_dump($data); 
}

Thanks

Please Log in to join the conversation.

1 month 1 week ago #337565 by krileon
Replied by krileon on topic CB field code dropdown single select

Could you tell me what is needed in the value/label of the global tab in order to get the data to be displayed ?

Those are for static options. Code based options are added under Parameters > Code. That fieldtype allows you to combine static options with code based ones.

Please be sure to read the description of the "Code (PHP)" param carefully as it explains how to output code based options. It expects an array of labels and values. An example of how the array should be formatted is included in the description. Below is also a few examples.

Format
return array( 'Value' => 'Label' );

Value and Label are the same
return array( '1', '2', '3', '4', '5' );

Value and Label are different
return array( '1' => 'Value 1', '2' => 'Value 2', '3' => 'Value 3', '4' => 'Value 4', '5' => 'Value 5' );

Option grouping is also supported using nested arrays. Example of this is provided in the description as well.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

1 month 1 week ago - 1 month 1 week ago #337567 by activha
Replied by activha on topic CB field code dropdown single select
Sorry but it does not work when the external php is included in the code field.
Tried return $models or echo json_encode($models) and nothing displays in the front end

My API is working fine, checked with postman and the code seems ok.

Maybe trying to write it directly in the code field would help but what can I use in substitution for $http = HttpFactory::getHttp(); and Uri::base() with Community builder ?

It's not possible to use the token use in the code field or joomla api

A log postman is for instance : {"data":["neuralDBModel"]}

If I use a single code field with echo json_encode($models); it displays : [{"Value":"neuralDBModel","Label":"neuralDBModel"}] but the dropdown single select would not work

Please Log in to join the conversation.

1 month 1 week ago #337568 by krileon
Replied by krileon on topic CB field code dropdown single select
I cannot help you debug your own custom code. See my above examples which do work. You need to output the expected format. You're outputting an array of JSON objects. That's not the expected format as provided above.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

1 month 1 week ago #337569 by activha
Replied by activha on topic CB field code dropdown single select
That was only for explanation purposes and to show that the API works fine !

The php code is 
<?php

use Joomla\CMS\Factory;
use Joomla\CMS\Http\HttpFactory;
use Joomla\CMS\Uri\Uri;

if (!file_exists(JPATH_SITE . '/libraries/CBLib/CBLib/Core/CBLib.php') || 
    !file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php')) {
    echo 'CB not installed';
    return;
}
// $token = '[cb_token_api]';
//$ndburl = '[cb_neuraldb]';

$ndata = [
    'ndb_url' => $ndburl,  
    'token' => $token
];

$url = Uri::base() . 'py/getNeuralDBModels'; 
$http = HttpFactory::getHttp();

$options = [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ]
];

$response = $http->post($url, http_build_query($ndata), $options);
$data = json_decode($response->body, true);

if (isset($data['data']) && is_array($data['data'])) {
    $models = [];
    foreach ($data['data'] as $model) {
        $models[] = ['Value' => $model, 'Label' => $model];
    }
    return $models; 
}  

This code works fine standalone but it fails in the CB code dropdown single select field.

So my question is : maybe that receiving only one value is an issue for CB code dropdown single select field ?

There is no reason that it fails

Please Log in to join the conversation.

1 month 1 week ago - 1 month 1 week ago #337570 by krileon
Replied by krileon on topic CB field code dropdown single select

$models[] = ['Value' => $model, 'Label' => $model];

That is not the format provided above. Please read my reply carefully as examples have been provided.

So my question is : maybe that receiving only one value is an issue for CB code dropdown single select field ?

No, it's an issue with your code. You're setting every option to have the same value. Below is the expected format.

return array( 'Value' => 'Label' );


This is just to show you that the value is the array key and the label is the array value. So lets say for example I want the following options.

Option 1
Value: 1
Label: Opt 1

Option 2
Value: 2
Label: Opt 2

To have this I would return the following.

return array( '1' => 'Opt 1', '2' => 'Opt 2' );


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.271 seconds

Facebook Twitter LinkedIn