phpoctobercmsoctobercms-plugins

OctoberCMS translate plugin with SEO plugin doesnt translate SEO keywords for CMS pages and Blogs


I am using OctoberCMS SEO translation plugin https://octobercms.com/plugin/anandpatel-seoextension and it works as per my expectations .. but I have a query with Translate plugin https://octobercms.com/plugin/rainlab-translate . Please see my couple of screenshots first.

enter image description here

enter image description here

As you can see in my above 2 screenshots, In blog pages while I am supposed to create or edit a blog, I am unable to make translation for Meta Title, Meta Description , Meta Keywords etc ..

Additionally, In my CMS pages also, I am unable to make my "Meta Keywords" translatable ..

I have tried to put below code in one of my active plugin's Plugin.php file to be able to make my Blog "Meta Title" field translatable, but it didnt work either ..

\RainLab\Blog\Models\Post::extend(function($model) {            
            $model->translatable[] = 'seo_title';
        });

And tried below code as well ..

\AnandPatel\SeoExtension\Models\BlogPost::extend(function($model) {
            $model->translatable[] = 'seo_title';
        });

Can someone guide me how can I make these fields translatable ?


Solution

  • I debugged It and The problem is

    SEO Extension Plugin is listening for \Event::listen('backend.form.extendFields' ... and this event is fired after backend.form.extendFieldsBefore so new added fields are not translatable.

    backend.form.extendFieldsBefore this event is responsible to convert field in to translatable field so fields which are added after this event are not shown as translatable.

    I am planning to correct this behavior and PR contribution to author repo https://github.com/anand-patel/oc-seo-extension once i get some free time

    Solution could be [ need to be done in plugin base code ] to use backend.form.extendFieldsBefore event in SEO Extension Plugin and during extension of fields it need to directly work with raw config, right now it uses $widget->addFields so it has to remove that and directly injecting fields to config so, later on newly added fields can be processed by RainLab.Translate Plugin using backend.form.extendFieldsBefore event.

    So For now workaround in your custom plugin add fields which are already have support for transnational or you can again use extendFieldsBefore and work with raw config to add fields.

    here we just use easy solution and add override field with translatable field type

    use RainLab\Blog\Models\Post;
    use Event;
    use System\Classes\PluginManager;
    
    public function register() {
    
      Post::extend(function($post) {
          if (!$post->propertyExists('translatable')) {
              $post->addDynamicProperty('translatable', []);
          }
          $post->translatable = array_merge($post->translatable, ['seo_title', 'seo_description' /* so on ....*/]);
      });
    
      Event::listen('backend.form.extendFields', function($widget) {
          if(PluginManager::instance()->hasPlugin('RainLab.Blog') && $widget->model instanceof \RainLab\Blog\Models\Post)
            {
                $widget->addFields([
                  'seo_title' => [
                      'label'   => 'Meta Title',
                      'type'    => 'mltext', //<- HERE
                      'tab'     => 'SEO'
                  ],
                  'seo_description' => [
                      'label'   => 'Meta Description',
                      'type'    => 'mltextarea', //<- HERE
                      'size'    => 'tiny',
                      'tab'     => 'SEO'
                  ],
                  'seo_keywords' => [
                      'label'   => 'Meta Keywords',
                      'type'    => 'mltextarea', //<- HERE
                      'size'    => 'tiny',
                      'tab'     => 'SEO'
                  ],
                  'canonical_url' => [
                      'label'   => 'Canonical URL',
                      'type'    => 'mltext', //<- HERE
                      'tab'     => 'SEO',
                      'span'    => 'left'
                  ],
                  'redirect_url' => [
                      'label'   => 'Redirect URL',
                      'type'    => 'mltext', //<- HERE
                      'tab'     => 'SEO',
                      'span'    => 'right'
    
                  ],
                  // ... so on
              ],
              'secondary');
            }
      });
    }
    

    but this is workaround for standard solution, I am planning to correct this and push PR to author plugin repo may be -> https://github.com/anand-patel/oc-seo-extension

    if any doubts please comment.