Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Ready to contribute?

Amazing! Sign our contributors' agreement and then join us on GitHub.

Update for critical security issue in PHPMailer included in release Vanilla 2.3.1

Code Snippets

R_JR_J Cheerleader & TroubleshooterMunich Moderator

There are some tasks I face more often when trying to write a plugin and I always have to search in order to get started. I do not want to write a full blown skeleton with each and every possibility, I favor a collection of useful snippets. That's why I've started this discussion where I will post some useful snippets without much explanation. Maybe some of you want to join in...

Swennet

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    First one is a skeleton for a page of the look and feel of a standard vanilla page

    public function VanillaController_MyPlugin_Create($Sender) {
       $Sender->MasterView = 'default';
    
       $Sender->ClearCssFiles();
       $Sender->AddCssFile('style.css');
       $Sender->AddCssFile('myplugin.css', 'plugins/MyPlugin');
    
       $Sender->AddModule('UserBoxModule');
       $Sender->AddModule('CategoriesModule');
       $Sender->AddModule('BookmarkedModule');
       $Sender->AddModule('GuestModule');
       $Sender->AddModule('NewDiscussionModule');
       $Sender->AddModule('DiscussionFilterModule');
       $Sender->AddModule('SignedInModule');
       $Sender->AddModule('CategoryFollowModule');
       $Sender->AddModule('DraftsModule');
       $Sender->AddModule('CategoryModeratorsModule');
       $Sender->AddModule('Ads');
    
       $Sender->SetData('Breadcrumbs', array(array('Name' => T('My Plugin'), 'Url' => '/vanilla/myplugin')));
    
       $Sender->Render(parent::GetView('myplugin.php'));
    }
    
  • Please can we get some more, awesomeness

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    I never posted any follow ups, but there are a lot of things I need more often...

    This one put at the end of a view will force a reload of the page after a popup has been closed.

    <script>
    var myFunc = $.popup.close;
    $.popup.close = function () {
      myFunc.apply(this, arguments);
      location.reload(); 
    };
    </script>
    

    Needful if you have a "edit" popup on a page and want the page to reload after you have made your changes

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
    edited December 2016

    Although using pluginController for the plugins setting screen is recommended, I prefer using the settingsController:

    A settings screen where you do some validations:

        public function settingsController_..._create($sender) {
            $sender->permission('Garden.Settings.Manage');
    
            $sender->addSideMenu('dashboard/settings/plugins');
    
            $sender->setData('Title', t('...'));
    
            $validation = new Gdn_Validation();
            $configurationModel = new Gdn_ConfigurationModel($validation);
            $configurationModel->setField(
                [
                    '....Field1',
                    '....Field2',
                    '....Field3',
                    ...
                ]
            );
            $sender->Form->setModel($configurationModel);
    
            if ($sender->Form->authenticatedPostBack() === false) {
                // If form is displayed "unposted".
                $sender->Form->setData($configurationModel->Data);
            } else {
                // Validate posted form.
                $sender->Form->validateRule('....Field1', 'ValidateRequired');
                $sender->Form->validateRule('....Field1', 'ValidateInteger');
                $sender->Form->validateRule('....Field2', 'ValidateRequired');
                // Enum validation.
                $field = (object)[];
                $field->Enum = ['Monday', 'Tuesday']; // etc
                $field->AllowNull = true;
                $sender->Form->validateRule('....Field3', ['Name' => 'ValidateEnum', 'Args' => $field]);
                // "Manual" errors:
                if (! some condition ) {
                    $sender->Form->setValidationResults(
                        [
                            '....Field2' => [
                                'Any number of error messages',
                                'provided as an array'
                            ]
                        ]
                    );
                }
    
                // Try saving values.
                if ($sender->Form->save() !== false) {
                    $sender->informMessage(
                        sprite('Check', 'InformSprite').t('Your settings have been saved.'),
                        ['CssClass' => 'Dismissable AutoDismiss HasSprite']
                    );
                }
            }
            $sender->render($this->getView('settings.php'));
        }
    
Sign In or Register to comment.