District Core Developer DocsDistrict Core Developer Docs
Developers
Boilerplate
Modules
Bitbucket
Developers
Boilerplate
Modules
Bitbucket
  • Modules

    • ABN
    • ActivityLog
    • AnalyticsApi
    • ApiConnector
    • BlockApi
    • CategoryApi
    • CloneApi
    • CommentApi
    • ContentApi
    • Core
    • Documents
    • EmbedApi
    • Event
    • ExportApi
    • FeatureApi
    • FormApi
    • GTM
    • GalleryApi
    • HelpApi
    • Hotspot
    • IdeaSurvey
    • ImportApi
    • InteractionsApi
    • Intercom
    • MailApi
    • MapApi
    • MapSurvey
    • MediaApi
    • MenuApi
    • MetaTagApi
    • NlpApi
    • NotificationApi
    • Page
    • ParentableContent
    • PaymentApi
    • PermissionsApi
    • Postcode
    • ReCaptcha
    • Redirects
    • Renderer
    • ReportApi
    • RestrictionApi
    • RevisionApi
    • SearchApi
    • Settings
    • ShareableApi
    • Slack
    • SlugApi
    • SubscribableApi
    • Survey
    • Team
    • TenantApi
    • TestApi
    • ThemeApi
    • Timeline
    • TranslationApi
    • Update
    • Users
    • VisualisationApi
    • WorkflowApi
    • Wysiwyg

CloneApi Module

The clone api module provides a generic approach to cloning and replicating a model. It uses bkwld/cloner package under the hood. This package allows us to replicate a model along with its relationships.

Usage

If you want any of your model to be cloned, it should first implement CanCloneInterface and use CanClone trait provided by this module.

  1. Use the CloneRouteService to register routes and CloneService to generate action urls.
// See survey and page module.

CloneRouteService::registerDashboardRoutes($yourController, 'your_prefix');
CloneService::generateCloneableActionUrls(
            $model,
            $baseParamsToBePassed,           
            $basePrefixToBePassed
        );

  1. Use the CloneApiDashboardControllerTrait in your model dashboard controller and use provided methods for the create and store logic.

  2. Create a new form request that extends DashboardCloneModelRequest. This form request should be used to validate incoming store request at controller level.

<?php

namespace Modules\*\Http\Requests\Dashboard;

use Modules\CloneApi\Http\Requests\Dashboard\DashboardCloneModelRequest;

class YourRequest extends DashboardCloneModelRequest
{
    /**
     * {@inheritdoc}
     */
    protected string $requestModel = YourModel::class;
}


That is it, this should be the bare minimum to get you started for a working instance.

Alternative use case

You may also want certain models to be replicated/cloned on the background without really needing all the boilerplate of having to register routes, urls, forms etc... For that use case, you can simply only implement the CanCloneInterface and use CanClone trait. Then call $yourModel->duplicate(). That should automatically clone the model for you along with relationships and other good stuffs mentioned below.

Cloning media

Any media attached to a model via media relationship will be automatically cloned and handled by this module. It will create new entries in the database and copy existing media files to the new cloned model.

Schema

By, default the clone schema consists of title and auto-injects slug and clone_blocks fields, but you can also customize and add more fields in the schema per model and its type.

<?php
// clone.php
return [
    /*
    |--------------------------------------------------------------------------
    | Customized schema per model.
    |--------------------------------------------------------------------------
    */
    // 'model_schema' => array_merge(config('clone.model_schema', []), [
    //        \Modules\ContentApi\Models\Content::class => [
    //            'default' => [],
    //            '[model_type]' => []
    //        ]
    //    ]),
];

Hooks

If you want to perform some operation while a model is being cloned. Then you can use these methods on your model.


/**
    use CanClone;
 
    /**
     * Do sth, before cloning this model.
     */
    public function beforeCloning(CanCloneInterface $src): void
    {       
       // Do sth here.
    }

    /**
     * {@inheritdoc}
     */
    public function onCloned(CanCloneInterface $src): void
    {      
        // Do sth here.
    }

Relationships

If you want to clone a relationship while cloning a model, define it as property on your model.

   protected $cloneable_relations = ['relation1', 'relation2'];

If you also want to hook into, the cloning events of the relationship itself, first implement the CanCloneRelationshipInterface. You can then use the hooks.

/**
     * {@inheritdoc}
     */
    public function onCloning(Model $src, ?bool $child = null): void
    {
        // Do sth.
    }

    /**
     * {@inheritdoc}
     */
    public function onCloned(Model $src): void
    {
        // Do sth.
    }

Customizing cloned attributes

By default id, created_at, updated_at,uuid, created_by_id, modified_by_id is not copied while cloning a model. You can add more if needed by specifying exempt attributes property on your model.

    protected $clone_exempt_attributes = ['column1', 'column2'];
Edit this page
Prev
CategoryApi
Next
CommentApi