How Do You Display Sample Code in API-Platform v3 - api-platform.com

I've seen a few examples of code samples being included in Redoc documentation and even tested other openapi.json files within API-Platform document file to prove that it can generate them.
What I cannot figure out is where to put x-code-samples inside the openapiContext to generate the Request samples.
This is what I would like to see in redoc.

The answer is I need to use x-codeSamples and not x-code-samples
#[ApiResource(
shortName: 'common-data',
operations: [
new Get(
uriTemplate: '/common-data/{id}',
openapiContext: [
'summary' => 'Returns commonly used static data for dropdowns etc',
'x-codeSamples' => [
['lang' => 'php', 'source' => 'php sample code'],
],
],
name: 'common-data_item',
),
],
)]
Then the sample will show up in the redocs.

Related

Laravel Nova upload image and save url to database

I have been attempting to upload an image to cloudinary, which is pretty easy. My problem is how do I go about saving the url to the database instead of the image? I was suppose to use https://github.com/Silvanite/nova-field-cloudinary, but the documentation is pretty slim. Also, I would like to save the image with original file name (storeOriginalName).
CloudinaryImage::make('Featured Image')
Nova's version:
Image::make('Featured Image')
->disk('cloudinary')
https://nova.laravel.com/docs/2.0/resources/file-fields.html#images
https://cloudinary.com/documentation/php_image_and_video_upload#upload_response
https://laravel.com/docs/5.7/requests#storing-uploaded-files
Update. This works for storing original file name, but still not sure how to grab url and save it to featured_image column:
CloudinaryImage::make('Featured Image')
->storeAs(function (Request $request) {
return $request->featured_image->getClientOriginalName();
}),
You shouldn't need to store the remote URL with Cloudinary. The public id returned by the component is used to generate the final URL when you output the image somewhere using one of the ways described in the documentation ...
// Using the helper (with transformation)
return cloudinary_image($this->featured_image, [
"width" => 200,
"height" => 200,
"crop" => "fill",
"gravity" => "auto",
])
// Using the Storage Facade (without transformation)
return Storage::disk('cloudinary')->url($this->featured_image);
// Using the Storage Facade (with transformation)
return Storage::disk('cloudinary')->url([
'public_id' => $this->featured_image,
'options' => [
"width" => 200,
"height" => 200,
"crop" => "fill",
"gravity" => "auto",
],
])
Or you could generate the URL yourself as per the Cloudinary documentation https://cloudinary.com/documentation/image_optimization
It would be helpful if you could expand on why you need to save the full URL as there may be an alternative solution.
The upload response contains an url field. Here is an example:
{
public_id: 'sample',
version: '1312461204',
width: 864,
height: 564,
format: 'jpg',
created_at: '2017-08-10T09:55:32Z',
resource_type: 'image',
tags: [],
bytes: 9597,
type: 'upload',
etag: 'd1ac0ee70a9a36b14887aca7f7211737',
url: '<url>',
secure_url: '<secure_url>',
signature: 'abcdefgc024acceb1c1baa8dca46717137fa5ae0c3',
original_filename: 'sample'
}

Why do inline/IRRE records get sys_language_uid=0?

When creating a tt_content record in sys_language_uid = 2 (no translation/l10n_parent = 0) in backend and inserting fields in a type => 'inline' column tx_foo_slider_slides (definition below), the newly created records in tx_foo_domain_model_slide are created with sys_language_uid = 0. Adding an image to the slide's image field creates a sys_file_reference with sys_language_uid = 2. Is this correct? If not, how do I change this? I would expected to have all records (tt_content, tx_foo_slider_slides, sys_file_reference) created with sys_language_uid = 2. It seems that records created before the update do have sys_language_uid set to 2, but I am not sure what has changed between 8 and 9: is it a core change? Or my site/language configuration? sys_language_uid had a default of 0 in TCA, but removing that and creating additional records did not show any different behaviour.
When trying to load records through an ExtBase repository where I seemingly have to use setRespectSysLanguage(false):
I do get no results with setLanguageOverlayMode(false)
I do get results with setLanguageOverlayMode(true), but the image field is NULL.
... but if I also manually change the tx_foo_domain_model_slide.sys_language_uid to 2 it looks fine in BE and FE/ExtBase (image is a working FileReference)
What could be going wrong? What might need to be changed? I think creating records in non-default languages is a supported case? For me, the root cause seems to be that records are created with the wrong language set.
My configuration:
TYPO3 9.5.5 (updated from 8)
config.tx_extbase.features.consistentTranslationOverlayHandling = 1 (but tested 0, too)
config.sys_language_overlay = 0 (I don't think 1 or hideNonTranslated changed any behaviour)
Sites (and multi-site). This specific site comes with two languages:
Language 1, German (languageId: '0') is disabled.
Language 2, English (languageId: '2') is enabled; fallbackType: strict
A custom table tx_foo_domain_model_slide including language fields and an image column:
'image' => [
'label' => $ll.'tx_foo_domain_model_slide.image',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'image',
[
'appearance' => [
'createNewRelationLinkTitle' => 'LLL:EXT:cms/locallang_ttc.xlf:images.addFileReference',
],
'overrideChildTca' => [
// types ...
],
'minitems' => 1,
'maxitems' => 1,
],
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] .',m4v,mp4v,mp4'
),
],
A column tx_foo_slider_slides in tt_content:
[
'label' => $ll . 'slider.slides',
'config' => [
'type' => 'inline',
'appearance' => [
'collapseAll' => true,
'expandSingle' => true,
],
'foreign_field' => 'slider',
'foreign_sortby' => 'sorting',
'foreign_table' => 'tx_foo_domain_model_slide',
'maxitems' => 100,
'minitems' => 0,
],
],
The problem in this case is, that tx_foo_domain_model_slide.sys_language_uid is defined as:
'sys_language_uid' => [
'config' => [
'type' => 'passthrough',
'default' => '',
],
],
According to the TCA docs:
FormEngine does not render anything for passthrough types by default. But it can be combined with a custom renderType to make it render something. A user type is better suited for such use cases, though.
Type passthrough field values are usually also not rendered at other places in the backend.
This leads to sys_language_uid not being set (and staying at 0). So, sys_language_uid has to be defined as 'type => 'select'` or similar. To hide it, one can put it in a hidden palette, as described here.

Creating a Gmail Draft with Recipients through Gmail API

I have been trying to figure out how to automatically add recipients to a Draft email that is created using the Gmail API through their Ruby library. I can create the draft without any issues but setting the recipients is causing me troubles and I haven't been able to find any good examples showing the best way to add email specific things.
Using the Google API playground and pulling in drafts that have already been created, it looks like the structure should be something similar to what is shown below, but whenever the draft is created, there are no recipients.
#result = client.execute(
:api_method => gmail.users.drafts.create,
:parameters => {
'userId' => "me"
},
:body_object => {
'message' => {
'raw' => Base64.urlsafe_encode64('Test Email Message'),
'payload' => {
'headers' =>
[
{
'name' => "To",
'value' => "John Smith <john_smith.fake#gmail.com>"
}
]
}
}
}
)
'raw' should contain the entire (RFC822) email, complete with body and headers. Do not use the 'payload.headers' structure, that parsed format is only used for returning during message.get() presently.
so for 'raw' you'd want to Base64.urlsafe_encode64() a string like:
"To: someguy#example.com\r\nFrom: myself#example.com\r\nSubject: my subject\r\n\r\nBody goes here"

send array of checkbox values to mailchimp via mailchimp api 2.0

I'm using this Mailchimp 2.0 PHP wrapper:
https://github.com/drewm/mailchimp-api
To send data to my list via the Mailchimp 2.0 API.
I can get email, firstname, and lastname to send successfully from my form to mailchimp.
I set those up as required fields in mailchimp(EMAIL, FNAME, LNAME).
Here is the PHP for that:
$MailChimp = new MailChimp('xxxxxxx');
$result = $MailChimp->call('lists/subscribe', array(
'id' => 'xxxxxx',
//required fields
'email' => array( 'email' => $_POST['email']),
'merge_vars' => array('FNAME' => $_POST['fname'], 'LNAME' => $_POST['lname']),
//mailchimp options
'double_optin' => false,
'update_existing' => true,
'replace_interests' => false
));
But I also have 12 checkboxes for stuff like engine size, type, gas type, color, etc. that are optional.
How can send these to the mailchimp API? I'm hoping someone with experience with Mailchimp API could help out.
Any help would be appreciated.
Thanks!
Here is a clarification to the structure of the groupings that is not in the docs' example. I.e. you supply only the names of the groups that have been selected by the user:
"merge_vars": {
"groupings": [
{
"groups": [
"selection 3",
"selection 7"
],
"id": <group_id>// or "name": <group_name>
}
]
},
In the merge_vars array, define "groupings" which points to an array. This 'groupings' array will then consist of individual arrays that point to a particular grouping of groups. Ex. if you have a grouping of groups titled "gas type" with group options "diesel", "unleaded", etc. this level of the array points to "gas type".
THEN, you define a "groups" array inside of this array to denote membership into the actual subgroups ("diesel", "unleaded").
Here's a code example from the list subscribe MailChimp API 2.0 documentation:
"merge_vars": {
"groupings": [
{
"id": 42,
"name": "example name",
"groups": [
"..."
]
}
]
lists/subscribe: http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php
My personal suggestion: create groups in the web app if you haven't already. Then, use the lists/interest-groupings method to see how the interest groups are formatted and returned to you. This gives you a sense of how to structure it in your own code.
lists/interest-groupings: http://apidocs.mailchimp.com/api/2.0/lists/interest-groupings.php

The merge tags in mandrill don't work in codeigniter

I use Mandrill plugin for Codeigniter.
I created HTML template through Mandrill account, named fess1 with merge tag FNAME, after I published it.
Example:
...
<p>
<span>Hi *|FNAME|*,<br></span>
</p>
....
Now I try to send mail from codeigniter like:
private function sendMailMandrill($owner_name,$business_name,$owner_email){
$message = array('dest_mail' => $owner_email);
$message['to'] = array(array('email' => 'mim#wefi.com'));
$mergeVars[] = array(
'rcpt' => array(array('email' => 'mim#wefi.com')),
'vars' => array(
array(
'name' => 'FNAME',
'content' => 'Fessy'
)
)
);
$message['merge'] = true;
$template_name = 'fess1';
$template_content = array( // I don't know what I need to provide here, left it empty
array(
'name' => 'example name',
'content' => 'example content'
)
);
$message['merge_vars'] = $mergeVars;
return $this->mandrill->messages_send_template($template_name, $template_content, $message);
}
The result:
I get the mail, based on fess1 template, but with the tag *|FNAME|*.
Sounds like Mandrill didn't recognize the merge tag.
I used mandrill->messages_send_template but since my template stored into Mandrill account I have no clue what I need to provide for $template_content.
So I wrote dummy info there.
Did I miss something?
Thank you,
[EDIT]
From logs this is what I send:
{
"template_name": "fess1",
"template_content": [
{
"name": "example name",
"content": "example content"
}
],
"message": {
"owner_name": "עידו",
"business_name": "פלאפל מוסקו",
"dest_mail": "maxim#wifi.com",
"to": [
{
"email": "maxim#wifi.com"
}
],
"merge": "true",
"merge_vars": [
{
"rcpt": [
{
"email": "maxim#wifi.com"
}
],
"vars": [
{
"name": "FNAME",
"content": "Fessy"
}
]
}
]
},
"key": "xxxxxxxxxxxxxxxx"
}
You can provide blank information for the template_content parameter. That parameter allows you to use mc:edit regions in your template. It is a required parameter, but a blank array will suffice if all of the content is in your template in Mandrill.
As for whether the merge_vars were recognized, the first thing we recommend is inspecting the API Logs for your account (Settings > API Logs) since that will show you the JSON that Mandrill received. You can then compare that to the expected JSON format from the Mandrill API docs: https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template
It looks like your arrays may not be nested as expected. Once you view the JSON that's being generated as compared with the expected format, you can also view the PHP documentation for the Mandrill PHP client. It may not be identical to the CodeIgniter plugin, but should give you an idea of how the merge_vars parameter would be structured in PHP: https://mandrillapp.com/api/docs/messages.php.html
In mergeVars you created array instead key:value. Change it to:
'rcpt' => 'mim#wefi.com',

Resources