How to link different media depending on language ?

  • Hi,

    I've created a custom module, called "News". Its inner workings are pretty similar to the "Post" module, but without the category and comments relationships. So it would be a plain post (title, content and attached media).

    What I want to achieve is to be able to link different media (in my case the relevant media is a video), for each language, in order to use it as a featured image. (Meaning the image would not be in the body of the news content).

    I know core modules shouldn't be modified, so I'm wondering which is the best approach to do so. Should I extend it -if possible-? Or should I create a custom media module? (using core Media module as a base and suit it for my needs).

    Many thanks,

    UPDATE: Please move this post to the correct category! (Module development)

  • Well, as no one proposed anything yet, I'll came up with my own solution.

    I think is not the best one, though...

    In order to touch the minimum amount of code and database relations, what I did is to create my own 'file-link' field on the media module, (calling it file-link-multilang [yes, perhaps is not the best name]). Instead of expecting directly a FileRepository object, I modified it to accept an array of FileRepositories, which I called $media[].

    Then in the controller in the edit function, as an example, this code will do (in this case the zone would be named video_preview_{locale})

    ///The code above is from the admin controller
    public function edit(Testimonials $testimonial)
        $availableLocales = json_decode($this->setting->get('core::locales'));
             foreach ($availableLocales as $locale) {
                  $media["video_preview_" . $locale] = 
                      $this->file->findFileByZoneForEntity('video_preview_' . $locale, $testimonial);
            return view('testimonials::admin.testimonials.edit', compact('testimonial', 'media'));

    After that, the Resources > views > admin > testimonials > partials > edit-fields.blade.php view, would have this structure:

    @include('media::admin.fields.file-link-multilang', [
        'entityClass' => 'Modules\\\\Testimonials\\\\Entities\\\\Testimonial',
        'entityId' => $testimonial->id,
        'zone' => 'video_preview_' . $lang

    Now when you swtich language tabs, you have a different zone for each different locale, thus being able to have different media for every translation. For instance, if your site is in english and german, and you want to link a featured video or image, you are not tied to have the same video/image for each language (e.g. one video in english and another in german).

    I know perhaps this is not the most optimal approach, but it kinda worked! :smirk:

  • admin

    Thank you for posting a solution! :)

  • @nWidart
    You welcome!

    But I have a doubt: would be this the optimal solution or just a workaround I figured out?

    Are there better approaches or ideas out there?

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.