Giter VIP home page Giter VIP logo

nova-dynamic-action-fields's Introduction

Dynamic Action Fields for Laravel Nova

This package allows you to change the fields of a Laravel Nova Action depending on which resources are selected.

This is experimental, proceed at your own risk

Screenshot

Screenshot of Dynamic Action Fields

Installation

composer require tunezilla/dynamic-action-fields

Usage

  1. Add the DynamicFieldAction trait to your resource

  2. Change your public function fields() to public function fieldsForModels(Collection $models): array

Example:

<?php

namespace App\Nova\Actions;

use App\User;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Fields\Text;
use TuneZilla\DynamicActionFields\DynamicFieldAction;

/**
 * Class ChangeEmail
 * @package App\Nova\Actions
 *
 * Example usecase of tunezilla/dynamic-action-fields
 */
class ChangeEmail extends Action
{
    use DynamicFieldAction;

    public function fieldsForModels(Collection $models): array
    {
        if ($models->isEmpty()) {
            return [];
        }

        /** @var User $user */
        $user = $models->first();

        return [
            Text::make('Email')
                ->help('The new email for ' . $user->name),
            $user->hasVerifiedEmail()
                ? Boolean::make('Require Verification')
                    ->help('Send a verification email that they must accept')
                : Hidden::make('Require Verification'),
        ];
    }

    public function handle(ActionFields $fields, Collection $models)
    {
        /** @var User $user */
        $user = $models->first();
        $user->email = $fields->get('email');

        if ($fields->get('require_verification')) {
            $user->email_verified_at = null;
        }

        $user->save();

        if (!$user->hasVerifiedEmail()) {
            $user->sendEmailVerificationNotification();
        }

        return Action::message(implode(' ', [
            'Changed email to ' . $fields->get('email'),
            $fields->get('require_verification')
                ? 'and sent a verification email'
                : 'but did not send a verification email'
        ]));
    }
}

nova-dynamic-action-fields's People

Contributors

tunezilla avatar

Stargazers

TheCodeholic avatar Bartłomiej Gajda avatar Mohamed Elkomy avatar  avatar Burak Sormageç avatar Paul Sharypov avatar Daniel Schweiger avatar Nicola Lazzaro avatar  avatar Richard Keep avatar Anatoli Nicolae avatar Nikolas Evers avatar Krishan König avatar Enric Vidal avatar  avatar Palaniappan P avatar Alberto avatar Sean avatar

Watchers

Sean avatar  avatar  avatar

nova-dynamic-action-fields's Issues

Nova 4

Will this work with Nova 4?

Syntax Error in DynamicFieldAction.php file (line 10)

Php is throwing the following error when running without changes

syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST) {"userId":99,"exception":"[object] (ParseError(code: 0): syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST) at /private/var/www/html/example-project/vendor/tunezilla/dynamic-action-fields/src/DynamicFieldAction.php:10)
[stacktrace]

This is the line causing the problem

public ?ActionRequest $actionRequest = null;

When I replace with the following

public function __construct(ActionRequest $actionRequest = null)
{
}

it works as expected.

Note:: error occurred running php 7.3.8. on 7.4.1 code ran without problem without the change

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.