ZF3 Multiple Controllers in same Module - zend-framework3

I am learning ZF3, i am trying to add InfoController to the Album module. would my URL be ....../album/info? I am getting 404 error occurred. I have seen John Deck's post and implemented exactly the same, but still not working

This is my module.config.php
<?php
namespace Album;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return array(
'controllers' => [
'factories' => [
Controller\InfoController::class => function($container) {
return new Controller\InfoController(
$container->get(\Album\Model\InfoTable::class)
);
},
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(\Album\Model\AlbumTable::class)
);
},
],
'aliases' => [
'index' => AlbumController::class,
'info' => InfoController::class,
]
],
'router' => array(
'routes' => array(
'album' => array(
'type' => Segment::class,
'options' => array(
'route' => '/album[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\AlbumController',//Controller::class,
'action' => 'index',
),
),
),
'info' => array(
'type' => Segment::class,
'options' => array(
//'route' => 'Album/Controller/Info[/:action[/:id]]',
'route' => '/InfoController[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Info',//::class
'action' => 'index',
),
),
),
),
),
'view_manager' => [
'display_not_found_reason' => false,
'display_exceptions' => false,
'doctype' => 'HTML5',
'template_map' => [
'layout/album' => __DIR__ . '/../view/layout/album_layout.phtml',
'album/album/index' => __DIR__ . '/../view/album/album/index.phtml',
'layout/album' => __DIR__ . '/../view/layout/info_layout.phtml',
'info/info/index' => __DIR__ . '/../view/info/info/index.phtml',
],
'template_path_stack' => [
'Album' => __DIR__ . '/../view',
'Info' => __DIR__ . '/../view',
],
],
);

Here's my Module.php
<?php
namespace Album;
use Album\Model\InfoTable;
use Album\Model\Info;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
Model\InfoTable::class => function($container) {
$tableGateway = $container->get(Model\InfoTableGateway::class);
return new Model\InfoTable($tableGateway);
},
Model\InfoTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Info());
return new TableGateway('info', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
public function getControllerConfig()
{
return array(
'factories' => array(
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
Controller\InfoController::class => function($container) {
return new Controller\InfoController(
$container->get(Model\InfoTable::class)
);
},
),
);
}
}

Related

How to add text that there are no search results for such entered word to a custom block?

I have a view that searches for indexed entity fields using context filters. I added a custom block to the view like this:
{{ drupal_block('result_entity_product_categories', {arguments}) }}
This block displays categories that match the entered word in the search. If you enter something for which there are no search results, for example, bbbbb, I need to display something like this:
Sorry
No results for: "bbbbb"
But here are some of our most popular products
P.S. The option to add text to the No Results Behavior view setting is not suitable. It is necessary to add text in the custom block.
The build() method code of my custom block:
public function build() {
$configuration = $this->getConfiguration();
$term = $configuration['arguments']['0'] ?: '';
if (empty($term)) {
return '';
}
$index = $this->entityTypeManager->getStorage('search_api_index')->load('entity_product_index');
$parse_mode = $this->parseModeManager->createInstance('terms');
$parse_mode->setConjunction('AND');
$search_query = $index->query();
$search_query->setParseMode($parse_mode)
->keys($term);
$search_result = $search_query->execute();
$rows = [];
foreach ($search_result->getResultItems() as $item) {
if (($node = $item->getOriginalObject()->getEntity()) && ($node instanceof NodeInterface)) {
$categoryKey = $node->get('field_entity_product_category')->getString();
if ($categoryKey) {
++$rows[$categoryKey];
}
}
}
$build['container'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['category-counter-wrapper'],
],
];
foreach ($rows as $key => $count) {
if ($node = $this->entityTypeManager->getStorage('node')->load($key)) {
$build['container'][$key] = [
'#type' => 'container',
'#attributes' => [
'class' => ['item'],
],
'label' => [
'#type' => 'container',
'#markup' => $node->getTitle(),
'#attributes' => [
'class' => ['label'],
],
],
'count' => [
'#type' => 'container',
'#markup' => $count,
'#attributes' => [
'class' => ['count'],
],
],
'link' => [
'#type' => 'link',
'#url' => Url::fromUserInput($node->get('field_custom_url')->getString(), ['query' => ['text' => $term]]),
'#attributes' => [
'class' => ['link'],
],
],
];
}
}
return $build;
}

Elementor access settings inside _register_controls function

I am using Elementor. In one section user can create categories with repeater field. In other section I would like to list these categories in Select2 control. How could I access category_data in _register_controls function? I know I can access it in render function.
protected function _register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => __( 'Content', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$repeater->add_control(
'category_title', [
'label' => esc_html__( 'Title', 'plugin-name'),
'type' => \Elementor\Controls_Manager::TEXT,
'label_block' => false,
]
);
$repeater->add_control(
'category_slug', [
'label' => esc_html__( 'Slug', 'plugin-name'),
'type' => \Elementor\Controls_Manager::TEXT,
'label_block' => false,
]
);
$this->add_control(
'category_data',
[
'label' => esc_html__( 'Category', 'plugin-name'),
'type' => \Elementor\Controls_Manager::REPEATER,
'fields' => $repeater->get_controls(),
'prevent_empty' => false,
'title_field' => '{{{ category_title }}}',
]
);
$this->end_controls_section();
$this->start_controls_section(
'category_section',
[
'label' => __( 'Category', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
//how could I access category_data here?
$settings = $this->get_settings_for_display();
$category_data = $settings['category_data'];
$cd_arr = [];
foreach ($category_data as $cd) {
$cd_arr[] = $cd['category_slug'];
}
if(count($cd_arr) > 0){
$this->add_control(
'media_category',
[
'label' => esc_html__( 'Categories', 'plugin-name'),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => $cd_arr,
]
);
}
$this->end_controls_section();
}

Drupal 8: how to generate options based on Ajax callback

In Drupal 8, I want to generate options of second select-box based on Ajax call of first select-box. The result generate a third new select box but I don't need to generate a new one. I want to replace the options of the second select-box. Please see my codes below:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['example_select'] = [
'#type' => 'select',
'#title' => $this->t('Select element'),
'wrapper' => 'first',
'#options' => [
'1' => $this->t('One'),
'2' => $this->t('Two'),
'3' => $this->t('Three'),
'4' => $this->t('From New York to Ger-ma-ny!'),
],
'#ajax' => [
'callback' => '::myAjaxCallback',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper' => 'edit-output',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Verifying entry...'),
],
]
];
$form['example_select2'] = [
'#type' => 'select',
'#title' => $this->t('Select element'),
'#prefix' => '<div id="first">',
'#suffix' => '</div>',
'#options' => [
],
'#ajax' => [
'callback' => '::myAjaxCallback2',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper' => 'edit-output',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Verifying entry...'),
],
]
];
return $form;
}
public function myAjaxCallback(array &$form, FormStateInterface $form_state) {
if ($selectedValue = $form_state->getValue('example_select')) {
$arr = array('1' => 'Nice way', '2' => 'Good way');
$form['example_select2']['#options'] = $arr;
}
return $form['example_select2'];
}
The below code is working well forme.
public function myAjaxCallback(array &$form, FormStateInterface $form_state) {
if ($selectedValue = $form_state->getValue('example_select')) {
$arr = array('1' => 'Nice way', '2' => 'Good way');
$form['example_select2']['#options'] = $arr;
}
$form_state->setRebuild(TRUE);
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand("#first", ($form['example_select2'])));
return $response;
}

Override not working on prestashop 1.7 Category class

Why is prestashop don't save my modification into database?
I've done an override on the Category.php (prestashop 1.7) class file, here is my code:
/override/classes/Category.php
class Category extends CategoryCore
{
/**
* afficher/masquer les produits
*/
public $show_products = 1;
public $andrana;
public function __construct($idCategory = null, $idLang = null, $idShop = null)
{
$definition = self::$definition;
$definition['fields']['show_products'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true);
$definition['fields']['andrana'] = array(
'type' => self::TYPE_HTML,
'lang' => true,
'validate' => 'isCleanHtml'
);
parent::__construct($idCategory, $idLang, $idShop);
}
}
/override\controllers\admin\AdminCategoriesController.php
class AdminCategoriesController extends AdminCategoriesControllerCore
{
public function renderForm()
{
$this->fields_form_override = array(
array(
'type' => 'switch',
'label' => $this->trans('Afficher les produits', array(), 'Admin.Global'),
'name' => 'show_products',
'required' => false,
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global')
)
)
),
array(
'type' => 'textarea',
'label' => $this->trans('Andrana', array(), 'Admin.Global'),
'name' => 'andrana',
'autoload_rte' => true,
'lang' => true,
'hint' => $this->trans('Invalid characters:', array(), 'Admin.Notifications.Info').' <>;=#{}'
),
);
return parent::renderForm();
}
}
PS: I've added "show_products" on ps_category table and "andrana" in ps_category_lang table
Please help me :(
change the __construct to:
public function __construct($idCategory = null, $idLang = null, $idShop = null)
{
self::$definition['fields']['show_products'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true);
self::$definition['fields']['andrana'] = array(
'type' => self::TYPE_HTML,
'lang' => true,
'validate' => 'isCleanHtml'
);
parent::__construct($idCategory, $idLang, $idShop);
}

My Node Type wont Show It's Fields

I am trying to create my first drupal module that creates a simple node type wcNames. This node type has two fields, firstname & lastname.
I could get my module create the DB table for node type and add it in node_type table, but when I open node/add/wc-name fields firstname and lastname don't appear.
Below is my code. Please suggest.
<?php
// wc_name.install file
/*
* hook_schema Implementation: Creates the DB and it's fields
*/
function wc_name_schema(){
$schema['wc_name_names'] = array(
'fields' => array(
'nmid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
'firstname' => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
'lastname' => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
),
'primary key' => array('nmid'),
'unique keys' => array(),
'indexes' => array(
'nmid' => array('nmid'),
),
);
return $schema;
}
//function wc_name_install(){ } // Use this function to set variables for the module.
function wc_name_uninstall(){
//unset variables if any set
// Delete all wc_name nodes
db_delete('node')->condition('type', 'wc_name')->execute();
// Delete all wc_name tables
db_drop_table('wc_name_names');
}
?>
<?php
// wc_name.module
/*
* Implementation of _node_info(): define node
*/
function wc_name_node_info(){
return array(
'wc_name' => array(
'name' => t('wcName'),
'base' => 'wc_name',
'base' => 'page',
'description' => t("A smaple module to save names."),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => FALSE,
)
);
}
function wc_name_menu() {
$items = array();
$items['wc_name'] = array(
'title' => 'wcNames',
'page callback' => 'wc_name',
// 'access arguments' => array('access wcNames'),
);
}
?>
<?php
// wc_name_form.inc
function wc_name_form($node, &$form_state){
// Add fields
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => FALSE,
'#default_value' => $node->wc_name['firstname'],
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => FALSE,
'#default_value' => $node->wc_name['lastname'],
);
return $form;
}
?>
What am I missing?
Try this:
function createTextFieldByName($type, $fieldname, $fieldlabel)
{
$field = field_info_field($fieldname);
if (empty($field)) {
$field = array(
'field_name' => $fieldname,
'type' => 'text',
'entity_types' => array('node'),
'translatable' => TRUE,
);
$field = field_create_field($field);
}
$instance = field_info_instance('node', $fieldname, $type);
if (empty($instance)) {
$instance = array(
'field_name' => $fieldname,
'entity_type' => 'node',
'bundle' => $type,
'label' => $fieldlabel,
);
$instance = field_create_instance($instance);
return $instance;
}
return null;
}
$type =
array(
'type' => 'MY_TYPE',
'name' => t('NAME'),
'base' => 'node_content',
'description' => t("DESCRIPTION"),
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);
createTextFieldByName($type,'firstname','First name');
createTextFieldByName($type,'lastname','Last name');

Resources