{"id":807,"date":"2023-01-03T08:23:00","date_gmt":"2023-01-03T08:23:00","guid":{"rendered":"https:\/\/www.webshouters.com\/blog\/?p=807"},"modified":"2023-10-05T06:47:16","modified_gmt":"2023-10-05T06:47:16","slug":"what-is-pagefactory-in-magento-2","status":"publish","type":"post","link":"https:\/\/www.webshouters.com\/blog\/what-is-pagefactory-in-magento-2\/","title":{"rendered":"What is PageFactory in Magento 2?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Page Factory is a design pattern used in Magento 2 that provides an easy way to manage web elements on a page. It acts as a model for creating pages, allowing developers to define elements and define methods for interacting with these elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say you want to interact with a &#8220;Sign In&#8221; button on the login page of your Magento 2 store. You can create a class with Page Factory to define the element and interact with it. Here&#8217;s how you might define the class in PHP:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">&lt;?php\nnamespace YourNameSpace\\Page;\n\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Framework\\View\\Element\\AbstractBlock;\nuse Magento\\Framework\\View\\Element\\UiComponent\\Control\\ButtonProviderInterface;\n\nclass LoginPage extends AbstractBlock implements ButtonProviderInterface\n{\n    \/**\n     * @var \\Magento\\Framework\\View\\Element\\UiComponent\\Control\\ButtonFactory\n     *\/\n    protected $buttonFactory;\n\n    \/**\n     * Constructor\n     *\n     * @param \\Magento\\Backend\\Block\\Widget\\Context $context\n     * @param \\Magento\\Framework\\View\\Element\\UiComponent\\Control\\ButtonFactory $buttonFactory\n     * @param array $data\n     *\/\n    public function __construct(\n        \\Magento\\Backend\\Block\\Widget\\Context $context,\n        \\Magento\\Framework\\View\\Element\\UiComponent\\Control\\ButtonFactory $buttonFactory,\n        array $data = []\n    ) {\n        $this-&gt;buttonFactory = $buttonFactory;\n        parent::__construct($context, $data);\n    }\n\n    \/**\n     * Define the element with Page Factory\n     *\n     * @return \\Magento\\Framework\\View\\Element\\UiComponent\\Control\\ButtonProviderInterface[]\n     *\/\n    public function getButtonData()\n    {\n        return [\n            &#039;label&#039; =&gt; __(&#039;Sign In&#039;),\n            &#039;class&#039; =&gt; &#039;signin&#039;,\n            &#039;id&#039; =&gt; &#039;signin-button&#039;,\n            &#039;on_click&#039; =&gt; sprintf(&quot;location.href = &#039;%s&#039;;&quot;, $this-&gt;getSignInUrl()),\n            &#039;sort_order&#039; =&gt; 10\n        ];\n    }\n\n    \/**\n     * Get sign in URL\n     *\n     * @return string\n     *\/\n    public function getSignInUrl()\n    {\n        return $this-&gt;getUrl(&#039;customer\/account\/login&#039;);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With Page Factory, you can now interact with the &#8220;Sign In&#8221; button by calling the <code>getButtonData()<\/code> method in your code. This makes it easier to manage elements on your page and reduces the risk of errors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Page Factory is a design pattern used in Magento 2 that provides an easy way to manage web elements on&#8230;<\/p>\n","protected":false},"author":3,"featured_media":869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,28],"tags":[30,11],"class_list":["post-807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento-2-tutorials","category-magento","tag-development","tag-magento-2"],"jetpack_featured_media_url":"https:\/\/www.webshouters.com\/blog\/wp-content\/uploads\/2023\/01\/Magento2.png","_links":{"self":[{"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/posts\/807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/comments?post=807"}],"version-history":[{"count":3,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions"}],"predecessor-version":[{"id":953,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/posts\/807\/revisions\/953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/media\/869"}],"wp:attachment":[{"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/media?parent=807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/categories?post=807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webshouters.com\/blog\/wp-json\/wp\/v2\/tags?post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}