Please upgrade here. These earlier versions are no longer being updated and have security issues.

Could anyopne make IndexDiscussionImage work in 2.8?

This discussion is related to the IndexDiscussionImage addon.

Looks like it doesnt detect the images embedded using the new editor, any idea on how to fix it?


Best Regards,

Igor

Comments

  • charrondevcharrondev Application Developer (PHP, JS) Montreal Vanilla Staff
    edited April 30

    It seems like it hasn't been updated in a minute.

    Tested on Vanilla 2.2

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    I've forked it and you can try that altered version


    Kasparigorneumann
  • Many thanks for taking your time to work on this... but unfortunately it still not working.

    It did get all the images from the posts created with the old editor, but ignores the ones created with the new editor.

  • charrondevcharrondev Application Developer (PHP, JS) Montreal Vanilla Staff

    @R_J I can assist with the rich editor parsing if you would like. Something like this would probably get you all of the image URLs from a rich post

    <?php
    /**
     * @author Adam Charron <[email protected]>
     */
    
    namespace Charrondev\IndexDiscussionAddon;
    
    use Vanilla\Formatting\Quill;
    
    /**
     * Format service for the rich editor format. Rendered and parsed using Quill.
     */
    class RichImageParser {
    
        /** @var Quill\Parser */
        private $parser;
    
        /**
         * Constructor for DI.
         *
         * @param Quill\Parser $parser
         */
        public function __construct(Quill\Parser $parser) {
            $this->parser = $parser;
        }
    
        /**
         * @inheritdoc
         */
        public function parseRichImages(string $richPost): array {
            $imageUrls = [];
            try {
                $operations = Quill\Parser::jsonToOperations($richPost);
                $blotGroups = $this->parser->parse($operations);
                /** @var Quill\BlotGroup $blotGroup */
                foreach ($blotGroups as $blotGroup) {
                    $blot = $blotGroup->getMainBlot();
                    if ($blot instanceof Quill\Blots\Embeds\ExternalBlot && $blot->getEmbedData()['type'] === 'image') {
                        $imageUrls[] = $blot->getEmbedData()['url'];
                    }
                }
                return $imageUrls;
            } catch (\Throwable $e) {
                // Handle/log invalid input here.
                return [];
            }
        }
    }
    

    That might not be perfect, but should be close to what's needed.

  • Many thanks for your help with this... but where should I place this code?

    Just add it to class.indexdiscussionimage.plugin.php?

  • Currently, the code is like this:

    <?php if (!defined('APPLICATION')) exit();

    $PluginInfo['IndexDiscussionImage'] = array(

      'Name' => 'IndexDiscussionImage',

      'Description' => "Adds the linked Images from the Discussions to the Discussions Index as a Sampler and provides a tooltip to preview the content.",

      'Version' => '2.1',

      'RegisterPermissions' => "Garden.Settings.Manage",

      'Author' => "VrijVlinder",

      'AuthorEmail' => '[email protected]',

       'AuthorUrl' => 'http://vrijvlinder.com',

      'License'=>"GNU GPL2"

    );

    class IndexDiscussionImagePlugin extends Gdn_Plugin {

      /**

      * Add style sheet.

      */

      public function Base_Render_Before($Sender) {

       $Sender->AddCssFile($this->GetResource('design/idi.css', FALSE, FALSE));

      }

      // Trigger on All Discussions.

      public function DiscussionsController_BeforeDiscussionContent_Handler($Sender) {

        $CssItem = $Sender->EventArguments['CssClass'];

        $CssItem = str_replace("Bookmarked"," ",$CssItem);

        $bodyLine = $Sender->EventArguments['Discussion']->Body;   

        $formline = strip_tags(str_replace(array('[',']'), array('<','>'), $bodyLine));  

        $sline = substr($formline, 0, 220) . "..." ;

        $oldName = $Sender->EventArguments['Discussion']->Name;

        $oldUrl = $Sender->EventArguments['Discussion']->Url;

        $ImageSrc = C('Plugin.IndexDiscussionImage.Image','/plugins/IndexDiscussionImage/design/images/default.png');

        preg_match('#\<img.+?src="([^"]*).+?\>#s', $Sender->EventArguments['Discussion']->Body, $images);

        if ($images[1]) {

          $ImageSrc = $images[1];

        }

        $newTitleAnchor = '<a class="IndexImage" href="' . $Sender->EventArguments['Discussion']->Url . '">' . Img($ImageSrc, array('title' => $sline, 'class' => "IndexImage")) . '</a>';

        echo "$newTitleAnchor";

      }

     

     //show in the specific category discussions list

      public function CategoriesController_BeforeDiscussionContent_Handler($Sender) {

        $CssItem = $Sender->EventArguments['CssClass'];

        $CssItem = str_replace("Bookmarked"," ",$CssItem);

        $bodyLine = $Sender->EventArguments['Discussion']->Body;   

        $formline = strip_tags(str_replace(array('[',']'), array('<','>'), $bodyLine));  

        $sline = substr($formline, 0, 220) . "..." ;

        $oldName = $Sender->EventArguments['Discussion']->Name;

        $oldUrl = $Sender->EventArguments['Discussion']->Url;

        $ImageSrc = C('Plugin.IndexDiscussionImage.Image','/plugins/IndexDiscussionImage/design/images/default.png');

        preg_match('#\<img.+?src="([^"]*).+?\>#s', $Sender->EventArguments['Discussion']->Body, $images);

        if ($images[1]) {

          $ImageSrc = $images[1];

        }

        $newTitleAnchor = '<a class="IndexImage" href="' . $Sender->EventArguments['Discussion']->Url . '">' . Img($ImageSrc, array('title' => $sline, 'class' => "IndexImage")) . '</a>';

        echo "$newTitleAnchor";

      }


      public function Base_GetAppSettingsMenuItems_Handler($Sender) {

       $Menu = $Sender->EventArguments['SideMenu'];

       $Menu->AddLink('Add-ons', 'IndexDiscussionImage', 'plugin/IndexDiscussionImage', 'Garden.Settings.Manage');

      }

     


     public function PluginController_IndexDiscussionImage_Create($Sender) {

         $Sender->Title('IndexDiscussionImage Plugin');

         $Sender->AddSideMenu('plugin/IndexDiscussionImage');

         $Sender->Form = new Gdn_Form();

         

         $this->Dispatch($Sender, $Sender->RequestArgs);

     }

       

      

     public function Controller_Index($Sender) {    

         

        $Sender->Permission('Garden.Settings.Manage');

        $Sender->SetData('PluginDescription',$this->GetPluginKey('Description'));

         

        

        $Sender->Form = new Gdn_Form();

        $Validation = new Gdn_Validation();

        $ConfigurationModel = new Gdn_ConfigurationModel($Validation);

        $ConfigurationModel->SetField(array(

          'Plugin.IndexDiscussionImage.Image'=> '/plugins/IndexDiscussionImage/design/images/default.png',

           

        ));

        $Sender->Form->SetModel($ConfigurationModel);



        if ($Sender->Form->AuthenticatedPostBack() === FALSE) {

          $Sender->Form->SetData($ConfigurationModel->Data);

        } else {

          $Data = $Sender->Form->FormValues();


          if ($Sender->Form->Save() !== FALSE)

            $Sender->StatusMessage = T("Your settings have been saved.");

        }


        $Sender->Render($this->GetView('idi-settings.php'));

      }

      

     

     public function Setup() {

       SaveToConfig('Plugin.IndexDiscussionImage.Image', '/plugins/IndexDiscussionImage/design/images/default.png');

       

     }






    }

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    It's no copy & paste job. The code above is a class. You have to know a few basics about PHP and OOP and Vanilla to use it.


  • Oh, thank you... Will try to research more about classes and see if I can manage to learn it.

Sign In or Register to comment.