Simple OctoberCMS Plugin Development

It's time to get outdoors and Explore

Create Plugin Scaffolding

php artisan create:plugin raju.h1bsalary
# ls
components  Plugin.php  updates

Plugin.php Details

<?php namespace Raju\H1bsalary;

use Backend;
use System\Classes\PluginBase;
use October\Rain\Database;
/**
 * h1bsalary Plugin Information File
 */
class Plugin extends PluginBase
{
    /**
     * Returns information about this plugin.
     *
     * @return array
     */
    public function pluginDetails()
    {
        return [
            'name'        => 'h1bsalary',
            'description' => 'No description provided yet...',
            'author'      => 'raju',
            'icon'        => 'icon-leaf'
        ];
    }

    /**
     * Register method, called when the plugin is first registered.
     *
     * @return void
     */
    public function register()
    {

    }

    /**
     * Boot method, called right before the request route.
     *
     * @return array
     */
    public function boot()
    {

    }

    /**
     * Registers any front-end components implemented in this plugin.
     *
     * @return array
     */
    public function registerComponents()
    {
       // return []; // Remove this line to activate

        return [
            'Raju\H1bsalary\Components\ContactForm' => 'contactForm',
        ];
    }

    /**
     * Registers any back-end permissions used by this plugin.
     *
     * @return array
     */
    public function registerPermissions()
    {
        return []; // Remove this line to activate

        return [
            'raju.h1bsalary.some_permission' => [
                'tab' => 'h1bsalary',
                'label' => 'Some permission'
            ],
        ];
    }

    /**
     * Registers back-end navigation items for this plugin.
     *
     * @return array
     */
    public function registerNavigation()
    {
        return []; // Remove this line to activate

        return [
            'h1bsalary' => [
                'label'       => 'h1bsalary',
                'url'         => Backend::url('raju/h1bsalary/mycontroller'),
                'icon'        => 'icon-leaf',
                'permissions' => ['raju.h1bsalary.*'],
                'order'       => 500,
            ],
        ];
    }
}

ContactForm.php Details

<?php

namespace Raju\H1BSalary\Components;

use Cms\Classes\ComponentBase;
use October\Rain\Database;
use DB;

class ContactForm extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name' => 'Contact Form',
            'description' => 'A simple contact form'
        ];
    }

    public function onSend()
    {
        // Get request data
        $data = \Input::only([
            'first_name',
            'last_name',
            'email',
            'content'
        ]);

        // Validate request
        $this->validate($data);

        // Send email
        $receiver = 'h1bsalary.online@gmail.com';

        \Mail::send('h1bslary.contact::contact', $data, function ($message) use ($receiver) {
            $message->to($receiver);
    });

    Db::table('contact')->insert([
         ['email' => $receiver, 'title' =>$receiver , 'message'=>json_encode($data)]
    ]);

    }

    protected function validate(array $data)
    {
        // Validate request
        $rules = [
            'first_name' => 'required|min:3|max:255',
            'last_name' => 'required|min:3|max:255',
            'email' => 'required|email',
            'content' => 'required',
        ];

        $validator = \Validator::make($data, $rules);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
    }

}

contact.htm page

<div class="container">
    <h1> Drop us a line </h1>
    {% component 'contactForm' %}
</div>

<form method="POST" 
    data-request="onSend"
    data-request-validate
    data-request-success="this.reset(); alert('Thank you for submitting your inquiry')"
>
    <div>

        <label for="first_name">First Name</label>
        <input type="text" name="first_name" class="form-control">
        <p data-validate-for="first_name" class="text-danger"></p> 
    </div>

    <div>
        <label for="last_name">Last Name</label>
        <input type="text" name="last_name" class="form-control">

        <p data-validate-for="last_name" class="text-danger"></p> 
    </div>

    <div>
        <label for="email">Email</label>
        <input type="text" name="email" class="form-control">
        <p data-validate-for="email" class="text-danger"></p> 
    </div>

    <div>
        <label for="content">Content</label>
        <textarea rows="6" cols="20" name="content" class="form-control"></textarea>
        <p data-validate-for="content"  class="text-danger"></p> 
    </div>

    <div>
        <button type="submit" class="btn btn-primary" data-attach-loading>Send</button>
    </div>
</form>

References
https://www.smashingmagazine.com/2019/10/developing-custom-plugin-october-cms/

https://octobercms.com/docs/database/query#inserts

https://medium.com/@bhadreshlaiya/plugin-development-october-cms-e1aa963f6b60

https://thoughts.depthinteractive.com/developing-a-simple-plugin-for-octobercms-61d1c7f5817d
https://octobercms.com/blog/post/mastering-components

https://www.sitepoint.com/octobercms-crud-building-a-teamproject-management-plugin/
https://github.com/Whyounes/OctoberCMS_Sitepoint_plugin_demo/

Posted in October CMS, OctoberCMS on Dec 13, 2020.


Comments

Please sign in to comment!