Alter fixedPostVars argument with custom userFunc - realurl

I have a realurl2 configuration which requires me to change the title of a given category by a selected language. The kicker is there is only one Database Record with the category title and the translation is made trough .xlf files.
I managed to replace the title now, however the mapping obviously does not work. Is there a method within the realurl Classes which could easily add a database record or am I stuck decoding it myself within the $_GET Paramaeters?
'fixedPostVars' => array(
// TODO: Implement dynamic via typoscript if possible!
'3' => array(
array(
'GETvar' => 'tx_products_products[product_categories]',
'userFunc' => function(&$params, $ref) use ($recordTranslator){
$categoryId = $params['value'];
$translation = $recordTranslator->render('Category', 'title', $categoryId, 'products');
$realUrlConf = new \DmitryDulepov\Realurl\Configuration\ConfigurationReader(0, $params);
$realUrlUtil = new \DmitryDulepov\Realurl\Utility($realUrlConf);
$translation = $realUrlUtil->convertToSafeString($translation);
return $translation;
}
),
This is what I have coded so far. The recordTranslator here just returns the String I want to use within the url.

I ended up pre filling my "tx_realurl_uniqalias" table with aliases, field, uid, language, etc.
The Translated Alias still was created trough
$realUrlConf = new \DmitryDulepov\Realurl\Configuration\ConfigurationReader(0, $params);
$realUrlUtil = new \DmitryDulepov\Realurl\Utility($realUrlConf);
$translation = $realUrlUtil->convertToSafeString($translation);
I mapped my Iso language tags to the spoken language prior to this. For that I created a typoscript object within my settings.
de {
ch-de = 1
de-de = 3
at-de = 5
}
en {
de-en = 4
}
fr {
ch-fr = 2
}
no {
no = 6
}
and saved the language field provided by the realurl_conf to filter the translation. Like so:
'GETvar' => 'L',
'valueMap' => array(
'ch-de' => '1',
'ch-fr' => '2',
'de-de' => '3',
'de-en' => '4',
'at-de' => '5',
'no' => '6',
),
'noMatch' => 'bypass',
I still have to clear my SpokenUrl Aliases within the backend whenever a new "Database Record" is created and rebuild it with the refreshed data but it works.
The lookup is the usual lookuptable format:
array(
'GETvar' => 'tx_products_products[product_categories]',
'lookUpTable' => array(
'table' => 'tx_products_domain_model_category',
'id_field' => 'uid',
'alias_field' => 'title',
'addWhereClause' => ' AND NOT deleted',
'useUniqueCache' => 1,
'useUniqueCache_conf' => array(
'strtolower' => 1,
'spaceCharacter' => '-',
),
),
),

Related

CakePHP 3.x - save not work in associate table

I have below table structure.
Mandator table
class MandatorTable extends Table
{
public function initialize(array $config)
{
$this->table('mandators');
$this->belongsToMany('Seminar', [
'foreignKey' => 'mandator_id',
'targetForeignKey' => 'seminar_id',
'joinTable' => 'mandators_seminars'
]);
}
}
Semiar table
class SeminarTable extends Table
{
public function initialize(array $config)
{
$this->table('seminars');
$this->belongsToMany('Mandator', [
'foreignKey' => 'seminar_id',
'targetForeignKey' => 'mandator_id',
'joinTable' => 'mandators_seminars'
]);
}
}
both table are belong to 'mandators_seminars' table
mandator_id, seminar_id
When I save data it's save in seminar table but not in 'mandators_seminars' table
Query
$seminartable = $this->Seminar->newEntity();
$this->request->data['mandator'][0] = 1;
$seminardata = $this->Seminar->patchEntity($seminartable, $this->request->data);
$this->Seminar->save($seminardata)
Request data
Array
(
[bookable] => test
[released] => aaa
[linkable] => bb
[name] => ccc
[internalnote] => ddd
[abstract] => ttt
[description] => ddd
[Category] => 14
[mandator] => Array
(
[0] => 1
)
[mandator_owner_id] => 1
)
Look you have two table Mandator and Similar as singular , but your connecting table is plural. Firstly check this. If there is still a problem check this CakePHP Through Association
As you can see your association should be like this:
$this->belongsToMany('Mandator', [
'foreignKey' => 'seminar_id',
'targetForeignKey' => 'mandator_id',
'through' => 'PluginName.MandatorsSeminars'
'joinTable' => 'mandators_seminars',
'className' => 'PluginName.Mandator'
]);
And one more tip: table should be called as plural.

Display Repeater sub-field, a post object value

I have a custom post_type 'publications' and it has a Repeater field 'linked_author' with a sub-field 'paper_linked_author' - a post object.
Here is the code to get all of the publications post and run a loop to get all authors linked to each post.
$args = array(
'posts_per_page' => '-1',
'post_type' => 'publications'
);
$the_query = new WP_Query( $args );
$post_ids = wp_list_pluck( $the_query->posts, 'ID' );
echo "<pre>";
print_r($post_ids);
echo "</pre>";
$artist_list = array();
foreach($post_ids as $post_id){
$artist_repeater = get_field('linked_author', $post_id);
echo "<pre>";
print_r($artist_repeater);
echo "</pre>";
if ($artist_repeater) {
$speakers = get_sub_field('paper_linked_author');
if ($speakers && count($speakers)>0)
{
foreach ($speakers as $speaker)
{
echo $speaker->ID; //$speaker is a post object//
}
}
}
}
I have been able to get all the sub-field values of each publication post however I'm not able to echo details of each linked author - the sub-field values. A print_r($artist_repeater) shows below details for one of the posts.
So, my question is how to list all unique authors from below print_r() array? Also, I need to link each author name such that clicking will bring list of all publications post he/she is linked to? Please help!
Array
(
[0] => Array
(
[paper_linked_author] => WP_Post Object
(
[ID] => 4885
[post_author] => 1
[post_date] => 2018-06-26 04:56:29
[post_date_gmt] => 2018-06-26 04:56:29
[post_content] =>
[post_title] => Q1-Test
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => q1-test
[to_ping] =>
[pinged] =>
[post_modified] => 2018-06-26 05:14:13
[post_modified_gmt] => 2018-06-26 05:14:13
[post_content_filtered] =>
[post_parent] => 0
[menu_order] => 0
[post_type] => people
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
[1] => Array
(
[paper_linked_author] => WP_Post Object
(
[ID] => 2115
[post_author] => 1
[post_date] => 2017-03-28 05:47:01
[post_date_gmt] => 2017-03-28 05:47:01
[post_content] =>
[post_title] => Julius Ceaser
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => julius-ceaser
[to_ping] =>
[pinged] =>
[post_modified] => 2018-06-25 11:02:55
[post_modified_gmt] => 2018-06-25 11:02:55
[post_content_filtered] =>
[post_parent] => 0
[menu_order] => 0
[post_type] => people
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
)
Just so it helps others, I solved the above problem with below code.
Basically, I was looking to list all authors who have written at least one Publication. So, earlier I was querying publications custom post_type and then looping each publication to list linked authors and that is where the above print_r() dump can be seen.
So, instead I queries all authors (a custom post_type) and then queries publications to match the key "linked_author_$_paper_linked_author" with the ID of the author post. It worked.
<?php
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$args = array(
'post_type' => 'people',
'posts_per_page' => -1, // Unlimited posts
'post_status' => 'publish'
// Order alphabetically by name
);
$loops = new WP_Query( $args );
while ($loops -> have_posts()) : $loops -> the_post();
$args = array(
'numberposts' => -1,
'post_type' => 'publications',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'linked_author_$_paper_linked_author',
'compare' => '=',
'value' => get_the_ID()
)
)
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
?>
<ul>
<li><?php echo the_title(); ?></li>
</ul>
<?php
endif;
endwhile;
?>

Drupal 8 how to save and render an image using a custom block configuration

I'm trying to create a custom module with configuration for a block that will allow a block to have custom fields. I'm having problems allowing the upload of an image and then rendering this in the block on the site.
Currently this is what my block file looks like;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'hello' block.
*
* #Block(
* id = "hello_block",
* admin_label = #Translation("Hello"),
* category = #Translation("Hello world block")
* )
*/
class HelloBlock extends BlockBase
{
/**
* {#inheritdoc}
*/
public function blockForm($form, FormStateInterface $formState)
{
$form['heading'] = array(
'#type' => 'textfield',
'#title' => t('Heading'),
'#description' => t('Enter the main heading'),
'#default_value' => 'Main heading'
);
$form['sub_heading'] = array(
'#type' => 'textfield',
'#title' => t('Sub heading'),
'#description' => t('Enter the sub heading'),
'#default_value' => 'Sub heading'
);
$form['body'] = array(
'#type' => 'text_format',
'#title' => t('Body'),
'#description' => t('Main body'),
'#format' => 'full_html',
'#rows' => 50,
'#default_value' => ''
);
$form['image'] = array(
'#type' => 'managed_file',
'#upload_location' => 'public://upload/hello',
'#title' => t('Image'),
'#upload_validators' => [
'file_validate_extensions' => ['jpg', 'jpeg', 'png', 'gif']
],
'#default_value' => isset($this->configuration['image']) ? $this->configuration['image'] : '',
'#description' => t('The image to display'),
'#required' => true
);
return $form;
}
/**
* {#inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $formState)
{
$this->configuration['heading'] = $formState->getValue('heading');
$this->configuration['sub_heading'] = $formState->getValue('sub_heading');
$this->configuration['body'] = $formState->getValue('body');
$this->configuration['image'] = $formState->getValue('image');
}
/**
* {#inheritdoc}
*/
public function build()
{
$markup = '<h1>'.$this->configuration['heading'].'</h1>';
$markup .= '<h2>'.$this->configuration['sub_heading'].'</h2>';
$markup .= '<img src="'.$this->configuration['image']['value'].'">';
$markup .= '<div>' . $this->configuration['body'] . '</div>';
return array(
'#type' => 'markup',
'#markup' => $markup,
);
}
}
Can anyone provide some pointers as to why the image isn't appearing? I'm assuming I'm missing something.
The text saved in the body (text_format) also appears in the block on the website as 'array', if anyone can help with that too it would be good, other wise I'll raise another question.
When you save that form, the value from the image field is the File ID.
Therefore, you can get the file object and the path by using:
$image = \Drupal\file\Entity\File::load($fid);
$path = file_create_url($image->getFileUri());
Then you would output the image using that path in your markup variable. There's probably a more semantic way to output the formatted image in a 'Drupal-ish' form, but this will get you started.
Oh, and the body field, use
$form_state->getValue('body')['value'];
in your markup.
BTW, I love using Devel and the ksm() function!

How to create an entity reference field which will allow unlimited values in a configuration form?

public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this->config('category_dynamic_block.settings');
$form['section_title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Section Title'),
'#description' => $this->t('Enter a Section Title'),
);
$form['term_name'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => array('categories'),
],
'#title' => $this->t('Term Name'),
'#description' => $this->t('Enter a Category Vocab Term Name'),
);
$form['page_title'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#selection_settings' => [
'target_bundles' => array('article'),
],
'#title' => $this->t('Page Title (' . $i . ')'),
'#description' => $this->t('Enter Page Title to be displayed'),
);
return $form;}
I'm creating a configuration form and I'm trying to find if there is a way in drupal 8 which will allow the user to enter multiple values for $form['page_title'] field.
This question (unlimited text fields with form api) may be what you are looking for: https://drupal.stackexchange.com/questions/208012/unlimited-textfields-with-form-api
Basically you'll need to add some ajax:
'#ajax' => [
'callback' => array($this, 'addMultipleItems'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Adding another item...'),
),
],

Can nusoap returns arrray of string?

I would like to return an array of string in my web services
I've tryed :
<?php
require_once('nusoap/nusoap.php');
$server = new soap_server();
$server->configureWSDL('NewsService', 'urn:NewsService');
$server->register('GetAllNews',
array(),
array('return' => 'xsd:string[]'),
'urn:NewsService',
'urn:NewsService#GetAllNews',
'rpc',
'literal',
''
);
// Define the method as a PHP function
function GetAllNews()
{
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
return $stack;
}
but it doesn't work.
What is the correct syntax for that ?
Thanks in advance for any help
You can't return an array like this. To return an array, you have to define a complex type.
I'll provide u an example...
The server program service.php:
<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('RM', 'urn:RM');
//Define complex type
$server->wsdl->addComplexType(
'User',
'complexType',
'struct',
'all',
'',
array(
'Id' => array('name' => 'Id', 'type' => 'xsd:int'),
'Name' => array('name' => 'Name', 'type' => 'xsd:string'),
'Email' => array('name' => 'Email', 'type' => 'xsd:string'),
'Description' => array('name' => 'Description', 'type' => 'xsd:string')
)
);
// Register the method
$server->register('GetUser', // method name
array('UserName'=> 'xsd:string'), // input parameters
array('User' => 'tns:User'), // output parameters
'urn:RM', // namespace
'urn:RM#GetUser', // soapaction
'rpc', // style
'encoded', // use
'Get User Details' // documentation
);
function GetUser($UserName) {
return array('Id' => 1,
'Name' => $UserName,
'Email' =>'test#a.com',
'Description' =>'My desc'
);
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
And the client program client.php:
<?php
// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the client instance
$client = new soapclient('http://localhost/Service/service.php');
// Call the SOAP method
$result = $client->call('GetUser', array('UserName' => 'Jim'));
// Display the result
print_r($result);
?>