I'm using #wordpress/server-side-render to get the content of the Gutenberg block from the backend side.
if ( props && props.attributes ) {
blockContent = <ServerSideRender
block="test/employee-block"
attributes={ props.attributes }
/>
}
and here is the PHP side of the block
register_block_type( 'test/employee-block', array(
'title' => 'Test Block',
'description' => 'test',
'category' => 'widgets',
'icon' => 'admin-users',
'api_version' => 2,
'editor_script' => 'employees_dynamic_block_script',
'render_callback' => function ($block_attributes, $content) {
return '<h1>test</h1>';
}
) );
However, the $block_attributes of the render callback function is always empty. I don't know why but according to the API documentation, it should be there.
Found the fix, if you do not define the argument key and type in the register_block_type function, it does not pass them to the render callback.
register_block_type( 'test/employee-block', array(
'api_version' => 2,
'editor_script' => 'employees_dynamic_block_script',
'attributes' => array(
'selectControl' => array(
'type' => 'string',
'default' => '0',
)
),
'render_callback' => function ($block_attributes, $content) {
return '<h1>test</h1>';
}
) );
Related
I've got a little problem with my code. I want to insert a new post with custom fields values, but these custom fields aren't from WordPress - they're from the theme. Simple solution like: update_meta_value() isn't working. I can't even find the theme script that sets values into these fields.
I found only that much of this code, what concern into my desired field:
array(
'id' => 'movie_default_fields',
'type' => 'select',
'class' => 'chosen',
'title' => esc_html__('Enable Default Fields', 'amy-movie'),
'options' => array(
'movie_release' => esc_html__('Release Date', 'amy-movie'),
'movie_duration' => esc_html__('Duration', 'amy-movie'),
'movie_imdb' => esc_html__('IMDB', 'amy-movie'),
'movie_mpaa' => esc_html__('MPAA', 'amy-movie'),
'movie_language' => esc_html__('Language', 'amy-movie'),
'movie_genre' => esc_html__('Genre', 'amy-movie'),
'movie_actor' => esc_html__('Actor', 'amy-movie'),
'movie_director' => esc_html__('Director', 'amy-movie'),
),
'attributes' => array(
'multiple' => 'multiple',
),
'default' => array('movie_release', 'movie_duration', 'movie_imdb', 'movie_mpaa', 'movie_language', 'movie_genre', 'movie_actor', 'movie_director')
),
if (!empty($custom_fields)) {
foreach ($custom_fields as $field) {
if ($field['type'] == 'text') {
$name = (isset($field['name']) && $field['name'] != '') ? $field['name'] : '';
$movie_field[] = array(
'id' => sanitize_title($name),
'type' => 'text',
'title' => $name,
);
}
}
}
It shows list of custom fields in specific custom post type:
if (!function_exists('amy_movie_defaults_fields')) {
function amy_movie_defaults_fields() {
$default = array(
'movie_release',
'movie_duration',
'movie_imdb',
'movie_mpaa',
'movie_language',
'movie_genre',
'movie_actor',
'movie_director'
);
return $default;
}
}
if (in_array('movie_duration', $defaults_fields)) {
$movie_field[] = array(
'id' => 'movie_duration',
'type' => 'text',
'title' => esc_html__('Duration', 'amy-movie'),
'after' => '<em>min</em>',
);
}
I'm trying to work with inserting these values while creating post automatically, but it's not working:
$post_id = wp_insert_post(array (
'post_type' => 'amy_movie',
'post_title' => $info->title,
'post_content' => $desc->description,
'post_status' => 'publish',
'movie_duration' => $info->duration, //simple example
));
The first thing I would do to troubleshoot this is put manual entries in to make sure you're using the wp_insert_post() function correctly. Try this by itself and see if it works. If it does, then you're just not pulling the values you want from the right places or in the right way:
$post_id = wp_insert_post(array (
'post_type' => 'amy_movie',
'post_title' => 'The Big LeBowski',
'post_content' => 'Movie Duration: 1h 5m',
'post_status' => 'publish',
//'movie_duration' => $info->duration, //try it without this first. This function would have to be overloaded by the theme for this to work.
));
When i try to add new documents to an index type , i loose existing documents which are overwritten by the new added ones . The problem can be related to the id of each added document ??
Here is the code :
$elasticaClient = new \Elastica\Client(array(
'host' => $this->container->getParameter('elastic_host'),
'port' => $this->container->getParameter('elastic_port')
));
$elasticaIndex = $elasticaClient->getIndex('app');
$elasticaIndex->create(
array(
'number_of_shards' => 4,
'number_of_replicas' => 1,
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'mySnowball')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'mySnowball')
)
),
'filter' => array(
'mySnowball' => array(
'type' => 'snowball',
'language' => 'German'
)
)
)
),
true
);
$elasticaType = $elasticaIndex->getType('type');
$mapping = new \Elastica\Type\Mapping();
$mapping->setType($elasticaType);
$mapping->setParam('index_analyzer', 'indexAnalyzer');
$mapping->setParam('search_analyzer', 'searchAnalyzer');
$mapping->setProperties(array(
'id' => array('type' => 'string'),
'title' => array('type' => 'string'),
'duration' => array('type' => 'string'),
'start' => array('type' => 'string'),
'end' => array('type' => 'string'),
));
// Send mapping to type
$mapping->send();
$documents = array();
foreach($medias as $media) {
$id = uniqid() ;
$documents[] = new \Elastica\Document(
$id,
array(
'id' => $id,
'title' => $media['title'],
'duration' => $media['duration'],
'start' => $media['start'],
'end' => $media['end'],
)
);
}
$elasticaType->addDocuments($documents);
$elasticaType->getIndex()->refresh();
Please i need your help . Thank you
PHP does not recommend using uniqid for this use case. Since you are wanting a random, safe id, let Elasticsearch do it for you. The Elastica Document construct method notes that the id field is optional. So don't pass it and let Elasticsearch issue the id.
Several things
$elasticaIndex->create (....) you only have to enter it once. Index is unique after creating the index that you can comment or generate a different index and other things. I leave an example that works.
class PersistencyElastic
{
private $conection;
public function __construct()
{
$this->conection = new \Elastica\Client(['host' => '127.0.0.1', 'port' => 9200]);
}
public function save($ msg)
{
// $ msg is an array with whatever you want inside
$index = $this->conection->getIndex('googlephotos');
// This is the index I created, it's called googlephotos
// $index->create(array (), true);
$type = $index->getType('googlephotos');
$type->addDocument(new Document (uniqid ('id _', false), $msg, $type, $index));
$index->refresh();
}
}
I am developing a module and I need to call different .tpl.php layouts from the theme directory per page.So what I currently have is:
return theme('adverts_index', array('adverts' => $advertsThemeArray));
That is what I return in one of the page callback functions. Now this works perfectly without a problem but in another page callback function within the module I attempt the same thing with a different theme hook but it does not call the layout specified by the theme hook.
$r = theme('adverts_view_advert', array(
'advert' => array(
'id' => $advert->aid,
'seller' => $advertiser,
'more_from_user' => array(),
'year' => $advert->year,
'condition' => $advert->condition,
'region' => $advert->region,
'city' => $advert->city,
'content' => $advert->description,
'bids' => $allBidsArray,
),
)
);
return $r;
The page is not complete blank, the main page.tpl.php which is used on every page which contains the header and what not does still display. Just the template I am trying to call to its contents is not displaying.
This is my hook_theme:
function adverts_theme() {
$theme = array();
$theme['adverts_index'] = array(
'template' => 'adverts-index',
'variables' => array(
'advert' => array(
'title' => null,
'formatted_price' => null,
'image' => null,
'permalink' => null,
'formatted_date' => null,
'description' => null,
),
),
);
$theme['adverts_view_advert'] = array(
'template' => 'adverts-single',
'variables' => array(
'advert' => array(
'id' => null,
'seller' => null,
'more_from_user' => null,
'year' => null,
'condition' => null,
'region' => null,
'city' => null,
'content' => null,
'bids' => null,
),
),
);
return $theme;
}
Any help would be highly appreciated!
arguments changed to variables in Drupal 7, and since either variables or render element is required the registry won't be picking up your theme.
Make the two changes in the hook_theme() implementation, clear the caches, and you should be good to go.
Having just arrived at Prestashop 1.5, I am making a very simple module: a video of the week, associated with multiple products that need to appear right next to it.
I decided to start from the Backoffice. Right now, I can view, add, edit and remove all the Video entries but I'm a bit lost on how to map the N-N association between a video and its related products... The lack of documentation isn't helping either.
Any ideas how to pull this off?
Here's a bit of my code, the Video class is defined by:
class Video extends ObjectModel {
public $id_video;
public $title;
public $url;
public $active;
public static $definition = array(
'table' => 'video',
'primary' => 'id_video',
'multilang' => false,
'fields' => array(
'id_video' => array(
'type' => ObjectModel :: TYPE_INT
),
'title' => array(
'type' => ObjectModel :: TYPE_STRING,
'required' => true
),
'url' => array(
'type' => ObjectModel :: TYPE_STRING,
'required' => true
),
'active' => array(
'type' => ObjectModel :: TYPE_BOOL,
'required' => true
)
),
);
(...)
and the AdminVideo class is here:
class AdminVideoController extends ModuleAdminController {
public function __construct()
{
$this->table = 'video';
$this->className = 'Video';
$this->lang = false;
$this->fields_list['id_video'] = array(
'title' => $this->l('ID'),
'align' => 'center',
);
$this->fields_list['title'] = array(
'title' => $this->l('Title'),
'width' => 'auto'
);
$this->fields_list['url'] = array(
'title' => $this->l('URL'),
'width' => 'auto'
);
$this->fields_list['active'] = array(
'title' => $this->l('Active'),
'width' => '70',
'align' => 'center',
'active' => 'status',
'type' => 'bool',
'orderby' => false
);
parent::__construct();
}
public function postProcess()
{
parent::postProcess();
}
public function renderList()
{
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->addRowAction('details');
return parent::renderList();
}
public function renderForm()
{
if (!($obj = $this->loadObject(true)))
return;
$this->fields_form = array(
'legend' => array(
'title' => $this->l('This weeks video'),
'image' => '../img/admin/world.gif'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Nome'),
'name' => 'title',
'size' => 33,
'required' => true,
'desc' => $this->l('Title')
),
array(
'type' => 'text',
'label' => $this->l('URL'),
'name' => 'url',
'size' => 33,
'required' => true,
'desc' => $this->l('Video URL')
),
array(
'type' => 'radio',
'label' => $this->l('Active:'),
'name' => 'active',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
'desc' => $this->l('Only one video can be active at any given time')
),
)
);
if (Shop::isFeatureActive())
{
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association:'),
'name' => 'checkBoxShopAsso',
);
}
$this->fields_form['submit'] = array(
'title' => $this->l(' Save '),
'class' => 'button'
);
if (!($obj = $this->loadObject(true)))
return;
return parent::renderForm();
}
}
One other thing: would it be possible to add a preview of the video inside the backoffice? I tried to echo YouTube's embed code, but it gets inserted even before the header. Is there a clean way of doing this or do I have to use some jQuery trickery? I was basically doing an echo of YT's embed code just before the end of postProcess().
Thanks in advance!
The simplest way to associate the videos to the products is by adding a "products" text field in your "video" table to store a comma separated list of the ids of the associated products (eg.: 1,10,27). Even if it's a bit rudimentary, it should work.
Alternatively, you could use a table like this:
create table video_product (
id_association int not null auto_increment,
id_video int,
id_product int,
primary key (id_association)
);
The problem with this solution is that the PrestaShop ObjectModel core does not provide any method to automatically update or delete the related tables (at least as far as I know), so you have to insert the code to manage the "video_product" table in your "Video" class.
If you want an example of how to do this, you should look at the classes/Product.php script, which manages the product table and all its related tables (categories, tags, features, attachments, etc.).
To have an idea of how the Prestashop database is structured, have a look at the docs/dbmodel.mwb file, which contains the schema of the database; this file can be viewed by using the MySQL Workbench application.
I need to build a Typoscript file in PHP and then pretty much execute the script to get the content of a given page.
This is my Code:
if (!defined('PATH_typo3conf')) die ('Could not access this script directly!');
class ContentHandler extends tslib_cObj
{
var $conf;
public function __construct()
{
tslib_eidtools::connectDB();
}
public function main()
{
$this->createConf(16);
$code = $this->cObjGet($this->conf);
echo "<pre>";
print_r($this->conf);
echo "</pre>";
echo "<pre>";
echo "code: " . $code;
echo "</pre>";
}
protected function createConf($pid)
{
$this->conf = array(
'foo' => 'CONTENT',
'foo.' => array(
'table' => 'tt_content',
'select.' => array(
'pidInList' => $pid,
'languageField' => 0,
),
'renderObj' => 'COA',
'renderObj.' => array(
'stdWrap.' => array(
'wrap' => '<b>|</b>',
),
'10' => 'TEXT',
'10.' => array(
'field' => 'header',
'wrap' => '<h3>|</h3>',
),
'20' => 'TEXT',
'20.' => array(
'field' => 'bodytext',
'wrap' => '<b>|</b>',
),
),
)
);
}
}
I believe the typosript is built well, but I don't get anything back. I check with a simple mysql_query() and it returns content. But I need to do this with typoscript.
Any help appreciated!
Edit: This not an actual extension script, but it's inside the EXT/ folder.
Your TypoScript is not correct. Have a look at the API (http://api.typo3.org/typo3v4/current/html/classtslib__c_obj.html#ab70d69a447f24f7416a85e7de1cb4ffb). Instead of "foo" you have to define an numerical key "10".
$this->conf = array(
'foo' => 'CONTENT',
'foo.' => array(
should do the job.
Btw.:
You need tslib_eidtools::connectDB(); only, if you are using an eID Script.