/** * Fill the form with the configuration data */ public function saveAction() { if (null !== ($response = $this->checkAuth(AdminResources::MODULE, [OpenGraph::DOMAIN_NAME], AccessManager::UPDATE))) { return $response; } // Create the form from the request $form = $this->createForm('open.graph.configuration.form'); // Initialize the potential error $error_message = null; try { // Check the form against constraints violations $validateForm = $this->validateForm($form); // Get the form field values $data = $validateForm->getData(); OpenGraph::setConfigValue(OpenGraphConfigValue::ENABLE_SHARING_BUTTONS, is_bool($data["enable_sharing_buttons"]) ? (int) $data["enable_sharing_buttons"] : $data["enable_sharing_buttons"]); foreach ($data as $name => $value) { ConfigQuery::write("opengraph_" . $name, $value, false, true); } // Redirect to the configuration page if everything is OK return $this->redirectToConfigurationPage(); } catch (FormValidationException $e) { // Form cannot be validated. Create the error message using // the BaseAdminController helper method. $error_message = $this->createStandardFormValidationErrorMessage($e); } if (null !== $error_message) { $this->setupFormErrorContext('configuration', $error_message, $form); $response = $this->render("module-configure", ['module_code' => 'OpenGraph']); } return $response; }
protected function buildForm() { $form = $this->formBuilder; $definitions = [["id" => "company_name", "label" => $this->trans("Your company's name"), "constraints" => []], ["id" => "twitter_company_name", "label" => $this->trans("Your company's name on twitter"), "constraints" => [new Callback(["methods" => [[$this, "verifyValue"]]])]], ["id" => "twitter_creator_name", "label" => $this->trans("The creator's name on twitter"), "constraints" => [new Callback(["methods" => [[$this, "verifyValue"]]])]]]; foreach ($definitions as $field) { $value = ConfigQuery::read("opengraph_" . $field["id"], ""); $form->add($field["id"], "text", array("constraints" => $field["constraints"], "data" => $value, "label" => $field["label"], "label_attr" => array("for" => $field["id"])))->add("enable_sharing_buttons", "checkbox", array("label" => "Enable the sharing buttons", "label_attr" => ["for" => "enable_sharing_buttons", "help" => Translator::getInstance()->trans('Check if you want to activate the sharing buttons in the front office', [], OpenGraph::DOMAIN_NAME)], "required" => false, "constraints" => array(), "value" => OpenGraph::getConfigValue(OpenGraphConfigValue::ENABLE_SHARING_BUTTONS, 1))); } }
public function openGraphSharingButtons(HookRenderEvent $event) { if (OpenGraph::getConfigValue(OpenGraphConfigValue::ENABLE_SHARING_BUTTONS, 0) == 1) { $acceptedTypes = ['category', 'product', 'folder', 'content']; $objectType = $this->getView(); if (in_array($objectType, $acceptedTypes)) { $event->add($this->render('open_graph_sharing_button.html', ['view_value' => $objectType])); } } }