cwsdigital/twill-metadata

Set metadata_translations.active = 1 programatically?

futurewebsites opened this issue · 1 comments

Hi TwillMetaPeeps,

Is there a way to set the value of metadata_translations active boolean to true / 1 pragmatically ?

for instance I am importing data using a custom artisan command like so:

 $import->metadata()->create([
                 'title' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_TITLE')),
                 'description' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_DESCRIPTION')),
                 'og_title' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_OG_TITLETITLE')),
                 'og_description' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_OG_DESCRIPTION')),
                 'og_type' => 'website',
                 'card_type' => 'summary',
                 'noindex' => 0,
                 'nofollow' => 0,
             ]);

$import represents the Model created during import but unless I manually go into the database and set the default value of active = 1 it throws an error - what would be nice would be to keep the default package schema and set the bool to true during the import.

thanks in advance

Hey, I think the trick to doing this would be to do your creation through the repository rather than the model.

Calling metadata()->create() is called directly on the model, and will blindly juts try and insert that attributes into the Metadata table - so I think you would find with your code above, that the attributes such as title, description etc... would not be saved on the translations table (and possibly not saved at all)

Whereas if you were to do

app(MatadataRepository::class)->create([ attributes... ])

This will use the repository, and as such use the HandleTranslations trait. You'd need to provide a locale for it to save the data to as well.

app(MetadataRepository::class)->create([ 
    'title' => [
                config('translatable.locale') => 'title',
    ],
])

The above would save to the currently active locale (I think) but you could obviously provide it manually, or provide multiple if needed.

As for setting the active field, this may depend on how many locales you have active. The code that would handle this from a Twill point of view would be prepareFieldsBeforeSaveHandleTranslations

From reading that, if you only have one locale it will automatically set the active field to true. If you have multiple locales, and one is already published, then it most liekly won't. Going this rate could be a bit complex, as the repositories are geared a little bit toward the data being formatted as it comes from the Twill UI.


The other way to approach this would be to query and update the translation model itself.
$import->metadata()->translation() would get you the MetadataTranslation modle for the current / default locale - which you could just update as any other model.

As this is a new object you'd probably need to use $import->metadata()->getTranslationOrNew( $locale ) you can pass a locale string, or null for current / default

So for your use case it might look something like the following (very untested!)

$import->metadata()->create([
                 'og_type' => 'website',
                 'card_type' => 'summary',
                 'noindex' => 0,
                 'nofollow' => 0,
             ]);

$metadataTranslation = $import->metadata()->getTranslationOrNew();

$metadataTranslation->update([
                 'title' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_TITLE')),
                 'description' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_DESCRIPTION')),
                 'og_title' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_OG_TITLETITLE')),
                 'og_description' => str_replace(['[SITENAME]','[BRAND]'],[$this->siteTitle,$this->device['product_brand']],env('META_BRAND_OG_DESCRIPTION')),
                 'active' => true,
]);

Hope that helps, beyond that all I can suggest is digging into the source code - browse through the HandleTranslations repository trait and the Translatable model trait to get an idea of what is happening under the hood. Twill uses Laravel Translatable so you can browse the docs for that too.