CRUD using TYPO3 MVC Framework:

We will start in this tutorial where we left off from the last one. We already have most of the stuff already done. The only thing left to do is add some new functionalities.

Let us first allow some new methods for this plugin.

Open [ext_localconf.php]

              
                <?php
                defined('TYPO3') or die('Access denied.');

                use TYPO3\CMS\Extbase\Utility\ExtensionUtility;
                use Contencance\YoutubeDemo\Controller\StoreInventoryController;

                /***************
                * Custom PLugins
                */
               ExtensionUtility::configurePlugin(
                   'youtube_demo',
                   'InventoryList',
                   [StoreInventoryController::class => 'list, edit, store, update, delete,'],
                   [StoreInventoryController::class => 'list, edit, store, update, delete,'],
               );
              
            

After this we will open controller and there we will add all the new methods we need. We want a "store" method so we can add a new entry in database. We also need a "edit" method to take us to a new page form with the info of the entry we want to modify, and "update" to update the info. Last a "delete" to delete the entry from database.

Open [StoreInventoryController.php]

              
                <?php

                namespace Contencance\YoutubeDemo\Controller;
                
                use Contencance\YoutubeDemo\Domain\Repository\ProductRepository;
                use Contencance\YoutubeDemo\Domain\Model\Product;
                use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
                
                /**
                 * Class StoreInventoryController
                 *
                 * @package MyVendor\StoreInventory\Controller
                 */
                class StoreInventoryController extends ActionController
                {
                
                    /**
                     * @var ProductRepository
                     */
                    private $productRepository;
                
                    /**
                     * Inject the product repository
                     *
                     * @param \MyVendor\StoreInventory\Domain\Repository\ProductRepository $productRepository
                     */
                    public function injectProductRepository(ProductRepository $productRepository)
                    {
                        $this->productRepository = $productRepository;
                    }
                
                    /**
                     * list Action
                     *
                     * @return void
                     */
                    public function listAction()
                    {
                        $products = $this->productRepository->findAll();
                        $this->view->assign('products', $products);
                    }
                
                    /**
                     * store Action
                     * 
                     * @return void
                     */
                    public function storeAction(Product $product)
                    {
                        $request = $_POST["tx_youtubedemo_inventorylist"]["product"]; 
                        $product->setName($request['name']);
                        $product->setDescription($request['description']);
                        $product->setQuantity($request['quantity']);
                        $this->productRepository->add($product);
                        return $this->redirect('list');
                    }
                
                    /**
                     * list Action
                     *
                     * @return void
                     */
                    public function editAction()
                    {
                        $id = $_POST["tx_youtubedemo_inventorylist"]["product"]["uid"]; 
                        $product = $this->productRepository->findByUid($id);
                        $this->view->assign('product', $product);
                    }
                
                    public function updateAction()
                    {
                        $request = $_POST["tx_youtubedemo_inventorylist"]["product"]; 
                        $product = $this->productRepository->findByUid($request['uid']);
                        $product->setName($request['name']);
                        $product->setDescription($request['description']);
                        $product->setQuantity($request['quantity']);
                        $this->productRepository->update($product);
                        return $this->redirect('list');
                    }
                
                        /**
                     * list Action
                     *
                     * @return void
                     */
                    public function deleteAction()
                    {
                        $id = $_POST["tx_youtubedemo_inventorylist"]["product"]["uid"]; 
                        $product = $this->productRepository->findByUid($id);
                        $this->productRepository->remove($product);
                        return $this->redirect('list');
                    }
                
                }
              
            

Now we add the views. We already have a view in [StoreInventory] [List.html]

Open [List.html]

              
                <div class="products-container">
                    <h2>Products:</h2>
                    <table class="table">
                       <thead>
                         <tr>
                           <th scope="col">#</th>
                           <th scope="col">Product name</th>
                           <th scope="col">Product description</th>
                           <th scope="col">Quantity</th>
                           <th scope="col">Edit</th>
                           <th scope="col">Delete</th>
                         </tr>
                       </thead>
                       <tbody>
                          <f:for each="{products}" as="product" key="index">
                             <tr>
                                <th scope="row">{index+1}</th>
                                <td>{product.name}</td>
                                <td><f:format.crop maxCharacters="100">{product.description}</f:format.crop></td>
                                <td>{product.quantity}</td>
                                <td>
                                   <f:form action="edit" controller="StoreInventory" objectName="product" method="post">
                                      <f:form.hidden property="uid" value="{product.uid}" />
                                      <f:form.button class="btn btn-warning">Edit</f:form.button>
                                   </f:form>
                                </td>
                                <td>
                                   <f:form action="delete" controller="StoreInventory" objectName="product" method="delete">
                                      <f:form.hidden property="uid" value="{product.uid}" />
                                      <f:form.button class="btn btn-danger">Delete</f:form.button>
                                   </f:form>
                                </td>
                              </tr>
                          </f:for>
                       </tbody>
                     </table>
                    <h2>Add New Product:</h2>
                    <f:form action="store" controller="StoreInventory" objectName="product" method="post">
                       <div class="form-group">
                          <label>Name</label><br>
                          <f:form.textfield property="name" class="form-control"/><br>
                          <label>Description</label><br>
                          <f:form.textarea property="description" class="form-control"/><br>
                          <label>Quantity</label><br>
                          <f:form.textfield property="quantity" class="form-control"/><br>
                          <f:form.button class="btn btn-success">Submit</f:form.button>
                       </div>
                    </f:form>
                </div>
              
            

Now finally let also create a html file for edit method to display the edit form. Create the [Edit.html] file in same folder as [List.html]

Open [Edit.html]

              
                <f:form action="update" controller="StoreInventory" objectName="product" method="put">
                    <div class="form-group">
                        <label>Name</label><br>
                        <f:form.textfield property="name" value="{product.name}" class="form-control"/><br>
                        <label>Description</label><br>
                        <f:form.textarea property="description" value="{product.description}" class="form-control"/><br>
                        <label>Quantity</label><br>
                        <f:form.textfield property="quantity" value="{product.quantity}" class="form-control"/><br>
                        <f:form.hidden property="uid" value="{product.uid}" />
                        <f:form.button class="btn btn-warning">Submit</f:form.button>
                    </div>
                </f:form>