1"use strict";(self.webpackChunkcoreshop_docs=self.webpackChunkcoreshop_docs||[]).push([[15746],{72392:(e,n,o)=>{o.r(n),o.d(n,{assets:()=>l,contentTitle:()=>d,default:()=>h,frontMatter:()=>i,metadata:()=>r,toc:()=>c});const r=JSON.parse('{"id":"CoreShop/Development/Extending_Guide/Extend_CoreShop_Forms","title":"Customizing Forms","description":"The forms in CoreShop are placed in the CoreShop\\\\Bundle\\\\BundleName\\\\Form\\\\Type namespaces and the extensions will be","source":"@site/versioned_docs/version-5.0/01_CoreShop/03_Development/01_Extending_Guide/02_Extend_CoreShop_Forms.md","sourceDirName":"01_CoreShop/03_Development/01_Extending_Guide","slug":"/CoreShop/Development/Extending_Guide/Extend_CoreShop_Forms","permalink":"/5.0/CoreShop/Development/Extending_Guide/Extend_CoreShop_Forms","draft":false,"unlisted":false,"tags":[],"version":"5.0","sidebarPosition":2,"frontMatter":{},"sidebar":"defaultSidebar","previous":{"title":"Extend CoreShop Resources","permalink":"/5.0/CoreShop/Development/Extending_Guide/Extend_CoreShop_Resources"},"next":{"title":"Extending CoreShop","permalink":"/5.0/CoreShop/Development/Extending_Guide/Extend_CoreShop_DataObjects"}}');var s=o(74848),t=o(28453);const i={},d="Customizing Forms",l={},c=[{value:"Why would you customize a Form?",id:"why-would-you-customize-a-form",level:2},{value:"How to customize a CoreShop Resource Form?",id:"how-to-customize-a-coreshop-resource-form",level:2}];function a(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,t.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"customizing-forms",children:"Customizing Forms"})}),"\n",(0,s.jsxs)(n.p,{children:["The forms in CoreShop are placed in the ",(0,s.jsx)(n.code,{children:"CoreShop\\Bundle\\*BundleName*\\Form\\Type"})," namespaces and the extensions will be\nplaced in ",(0,s.jsx)(n.code,{children:"App\\CoreShop\\Form\\Extension"}),"."]}),"\n",(0,s.jsx)(n.h2,{id:"why-would-you-customize-a-form",children:"Why would you customize a Form?"}),"\n",(0,s.jsx)(n.p,{children:"There are plenty of reasons to modify forms that have already been defined in CoreShop. Your business needs may\nsometimes slightly differ from our internal assumptions."}),"\n",(0,s.jsx)(n.p,{children:"You can:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["add completely ",(0,s.jsx)(n.strong,{children:"new fields"}),","]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"modify"})," existing fields, make them required, change their HTML class, change labels etc.,"]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:"remove"})," fields that are not used."]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"how-to-customize-a-coreshop-resource-form",children:"How to customize a CoreShop Resource Form?"}),"\n",(0,s.jsxs)(n.p,{children:["If you want to modify the form for the ",(0,s.jsx)(n.code,{children:"Store"})," in your system there are a few steps that you should take. Assuming that\nyou would like to (for example):"]}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["Add a ",(0,s.jsx)(n.code,{children:"contactHours"})," field,"]}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"These will be the steps that you will have to take to achieve that:"}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"1."})," If your are planning to add new fields remember that beforehand they need to be added on the model that the form\ntype is based on."]}),"\n",(0,s.jsxs)(n.p,{children:["In case of our example if you need to have the ",(0,s.jsx)(n.code,{children:"contactHours"})," on the model and the entity mapping for the ",(0,s.jsx)(n.code,{children:"Store"}),"\nresource. To get to know how to prepare that go ",(0,s.jsx)(n.a,{href:"/5.0/CoreShop/Development/Extending_Guide/Extend_CoreShop_Resources",children:"there"}),"."]}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"2."})," Create a ",(0,s.jsx)(n.strong,{children:"Form Extension"}),"."]}),"\n",(0,s.jsx)(n.p,{children:"Your form has to extend a proper base class. How can you check that?"}),"\n",(0,s.jsxs)(n.p,{children:["For the ",(0,s.jsx)(n.code,{children:"StoreType"})," run:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-bash",children:"$ php bin/console debug:container coreshop.form.type.store\
1n"})}),"\n",(0,s.jsxs)(n.p,{children:["As a result you will get\nthe ",(0,s.jsx)(n.a,{href:"https://github.com/coreshop/CoreShop/blob/master/src/CoreShop/Bundle/StoreBundle/Form/Type/StoreType.php",children:(0,s.jsx)(n.code,{children:"CoreShop\\Bundle\\StoreBundle\\Form\\Type\\StoreType"})})," -\nthis is the class that you need to be extending."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-php",children:"<?php\n\nnamespace App\\CoreShop\\Form\\Extension;\n\nuse CoreShop\\Bundle\\StoreBundle\\Form\\Type\\StoreType;\nuse Symfony\\Component\\Form\\AbstractTypeExtension;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType;\nuse Symfony\\Component\\Form\\FormBuilderInterface;\n\nfinal class StoreTypeExtension extends AbstractTypeExtension\n{\n public function buildForm(FormBuilderInterface $builder, array $options): void\n {\n // Adding new fields works just like in the parent form type.\n $builder->add('contactHours', TextType::class, [\n 'required' => false\n ]);\n }\n\n public static function getExtendedTypes(): array\n {\n return [StoreType::class];\n }\n}\n"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"3."})," After creating your class, register this extension as a service in the ",(0,s.jsx)(n.code,{children:"config/services.yaml"}),":"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-yaml",children:"services:\n App\\CoreShop\\Form\\Extension\\StoreTypeExtension:\n tags:\n - { name: form.type_extension, extended_type: CoreShop\\Bundle\\StoreBundle\\Form\\Type\\StoreType }\n"})}),"\n",(0,s.jsxs)(n.p,{children:["In our case you will need to extend the ExtJs Form as well: ",(0,s.jsx)(n.code,{children:"public/coreshop/js/store.js"}),"."]}),"\n",(0,s.jsxs)(n.p,{children:["In ",(0,s.jsx)(n.strong,{children:"ExtJs"})," your new store file need to look like this:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-javascript",children:"coreshop.store.item = Class.create(coreshop.store.item, {\n\n getFormPanel: function ($super) {\n var panel = $super();\n\n panel.down(\"fieldset\").add(\n [\n {\n xtype: 'textfield',\n fieldLabel: 'Contact Hours',\n name: 'contactHours'\n }\n ]\n );\n\n return this.formPanel;\n }\n});\n"})}),"\n",(0,s.jsx)(n.p,{children:"And you need to configure it to be loaded as well:"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-yaml",children:"core_shop_store:\n pimcore_admin:\n js:\n custom_store: '/coreshop/js/store.js'\n"})})]})}function h(e={}){const{wrapper:n}={...(0,t.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(a,{...e})}):a(e)}},28453:(e,n,o)=>{o.d(n,{R:()=>i,x:()=>d});var r=o(96540);const s={},t=r.createContext(s);function i(e){const n=r.useContext(t);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function d(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),r.createElement(t.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.