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.
Related
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>';
}
) );
Let's say I have an array that looks like this:
$args = array(
'format' => 'file',
'type' => 'page',
'name' => 'Projects',
'template' => 'default'
);
To get the values from the array keys I need to do this:
echo $args['format'];
echo $args['type'];
echo $args['name'];
echo $args['template'];
While it works, it feels more messy than needed. Is there a proper way to somehow convert it to this?
echo $format;
echo $type;
echo $name;
echo $template;
I'm aware of list but that only uses values and not keys.
Are you looking for http://php.net/manual/en/function.extract.php ?
$args = array(
'format' => 'file',
'type' => 'page',
'name' => 'Projects',
'template' => 'default'
);
extract($args);
echo "$format, $type, $name, $template";
Output:
file, page, Projects, default
We can easily print the array value using foreach() loop if we don't use to list() as you mention then use extract() method like below:
$args = array(
'format' => 'file',
'type' => 'page',
'name' => 'Projects',
'template' => 'default'
);
extract($args);
echo $format."<br/>".$type."<br/>".$name."<br/>".$template;
So I have a function called vendorGet() and it contains an array of vendor names and their logos. I call the vendorGet() function in another array and pass it a string argument to tell it which vendor to output from the array of vendors. Does doing this cause the PHP script to create this array every time the function is called, or does it create it once within the function and reference itself each time?
Hope that made sense. Let me know if there is anything I can clarify.
Here is some (shortened down and simplified) code as a reference,
// Vendor Fetcher
function vendorGet($data)
{
$vendor = array(
'foo' => array(
'name' => 'Foo Products',
'image' => (VENDOR_IMG . 'foo/foo-logo.png'),
),
'bar' => array( // ABT
'name' => 'Bar Inc.',
'image' => (VENDOR_IMG . 'bar/bar-logo.png'),
),
);
return $vendor[$data];
}
// Array calling vendorGet() function
$catalogue = array(
'chapter-1' => array(
'vendors' => array(
1 => vendorGet('foo'),
2 => vendorGet('bar'),
),
),
);
The code you have written will re-create the array every single time the function is called.
You can create the array outside of this function and still have the function reference the array if it's within a class using "this" or you can pass in the array as a variable to the function.
In short, the array will be set up every time the function is called because you're defining it within the function
If you want to avoid this you can use a class
class vendor {
public static $vendor = array(
'foo' => array(
'name' => 'Foo Products',
'image' => (VENDOR_IMG . 'foo/foo-logo.png'),
),
'bar' => array( // ABT
'name' => 'Bar Inc.',
'image' => (VENDOR_IMG . 'bar/bar-logo.png'),
),
);
public static function get($data) {
return self::$vendor[$data];
}
}
You don't need a function to do this. Arrays allow you to access by key.
// Vendor Array
$vendor = array(
'foo' => array(
'name' => 'Foo Products',
'image' => (VENDOR_IMG . 'foo/foo-logo.png'),
),
'bar' => array( // ABT
'name' => 'Bar Inc.',
'image' => (VENDOR_IMG . 'bar/bar-logo.png'),
),
);
// Array getting vendor by key
$catalogue = array(
'chapter-1' => array(
'vendors' => array(
1 => $vendor['foo'],
2 => $vendor['bar'],
),
),
);
Arrays allow you to access a key and return its corresponding value. You can also can also pull deeper into that array like this:
echo $vendor['foo']['name'];
This will output:
Foo Products
To clarify:
PHP has exactly two scopes: "local" and "global." Any variable declared outside of a function is "global." Any variable declared within a function is "local," unless the global directive is used to say that it is, in fact, global.
PHP fully supports recursion: a function may (directly or indirectly) call itself. The local variables of each active instance of that function are private to that instance.
Variables are normally passed into functions "by value," which means that a duplicate is made.
If you have many related functions that need to share common data, consider making a class, with the functions being "methods" of that class. Now, they all have access to $this, which refers to the object-instance to which they belong. Notice also that properties (and methods) can be declared either public or private, which is an effective way to keep other parts of the code from seeing (and therefore, possibly meddling with ...) them.
Yes it creates the array each time you call the function.
Performing a memory usage asserts it.
<?php
function vendorGetArray()
{
return array(
'foo' => array(
'name' => 'Foo Products',
'image' => ( 'foo/foo-logo.png'),
),
'bar' => array( // ABT
'name' => 'Bar Inc.',
'image' => ( 'bar/bar-logo.png'),
),
);
}
function memoryUsage()
{
echo "memory: ".(memory_get_peak_usage(false)/1024/1024)." MiB<br>";
}
$vendorArray = vendorGetArray();
// Array calling vendorGet() function
$catalogue = array(
'chapter-1' => array(
'vendors' => array(
1 => $vendorArray['foo'],
2 => $vendorArray['bar']
),
),
);
memoryUsage();
?>
The above code prints:
memory: 0.231658935547 MiB
While your code
<?php
function vendorGet($data)
{
$vendor = array(
'foo' => array(
'name' => 'Foo Products',
'image' => ( 'foo/foo-logo.png'),
),
'bar' => array( // ABT
'name' => 'Bar Inc.',
'image' => ( 'bar/bar-logo.png'),
),
);
return $vendor[$data];
}
function memoryUsage()
{
echo "memory: ".(memory_get_peak_usage(false)/1024/1024)." MiB<br>";
}
// Array calling vendorGet() function
$catalogue = array(
'chapter-1' => array(
'vendors' => array(
1 => vendorGet('foo'),
2 => vendorGet('bar'),
),
),
);
memoryUsage();
would print:
memory: 0.231948852539 MiB
I have two stores using Prestashop. I would like to import a products URLs list from the first to the second.
I can access to the product list by using http://example.com/api/products
I can also access to the product information by using
http://example.com/api/products/{ProductID}
By this way I can access to all products data but I can't find product URL.
Is there a way to retrieve a product URL from Prestashop ?
You can generate the product URL from the product ID:
$productUrl = 'http://mydomain.com/index.php?controller=product&id_product=' . $productId;
If Friendly URL is turned on then the URL will be rewritten.
For those who are looking to generate the absolute url inside their store, you can do the following :
$product = new Product(Tools::getValue('id_product'));
$link = new Link();
$url = $link->getProductLink($product);
Will result with something like :
http://your.prestashop-website.com/fr/1-T-shirts-a-manches-courtes-delaves.html
In prestashop > 1.6 you can proceed :
Override product class,
Redefine the definition schema and the webserviceParameters schema
Add both fields "url"
create a function "getWsUrl()" that returns the absolute url of your product
And the job is done, here is my code:
protected $webserviceParameters = array(
'objectMethods' => array(
'add' => 'addWs',
'update' => 'updateWs'
),
'objectNodeNames' => 'ProductForWs',
'fields' => array(
'id_default_image' => array(
'getter' => 'getCoverWs',
'setter' => 'setCoverWs',
'xlink_resource' => array(
'resourceName' => 'images',
'subResourceName' => 'products'
)
)
),
'associations' => array(
'url' => array('resource' => 'url',
'fields' => array(
'url' => array('required' => true)
),
'setter' => false
)
),
);
public static $definition = array(
'table' => 'product',
'primary' => 'id_product',
'fields' => array(
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'url' => array('type' => self::TYPE_STRING),
'associations' => array(),
),
);
public function getWsUrl(){
$link = new Link();
$product = new Product($this->id);
return array(array("url" => $link->getProductLink($product)));
}
The WebServiceOutputBuilder will call your function and return the url path as an association. Like:
<associations>
<url nodeType="url" api="url">
<url>
<url>
<![CDATA[ http://prestashop.dev/14-prod.html ]]>
</url>
</url>
</url>
</associations>
On PrestaShop™ 1.4.5.1
I use
public function getProductLink($id)
{
global $cookie;
$productUrl = "http://".$_SERVER['HTTP_HOST'].'/product.php?id_product=' . $id;
return $productUrl;
}
In add to #robin-delaporte you can use this overriding Product class and automatically get for all languages in your prestashop.
protected $webserviceParameters = array(
'objectMethods' => array(
'add' => 'addWs',
'update' => 'updateWs'
),
'objectNodeNames' => 'ProductForWs',
'fields' => array(
'id_default_image' => array(
'getter' => 'getCoverWs',
'setter' => 'setCoverWs',
'xlink_resource' => array(
'resourceName' => 'images',
'subResourceName' => 'products'
)
)
),
'associations' => array(
'url' => array(
'resource' => 'url',
'fields' => array(
'url' => array()
),
'setter' => false
),
),
);
public function getWsUrl(){
$languages = Language::getLanguages(true, $this->context->shop->id);
$link = new Link();
$product = new Product($this->id);
if (!count($languages))
return array(array("url" => $link->getProductLink($product)));;
$default_rewrite = array();
$rewrite_infos = Product::getUrlRewriteInformations($this->id);
foreach ($rewrite_infos as $infos)
$default_rewrite[$infos['id_lang']] = $link->getProductLink($this->id, $infos['link_rewrite'], $infos['category_rewrite'], $infos['ean13'], (int)$infos['id_lang']);
return $default_rewrite;
}
Define $your_product_id in the controller file of the tpl file, then call it from the tpl file as follows
yourcontroller.php
public function hookTheHookYouWant()
{
$set_product = $this->context->smarty->assign(array(
'product_id' => 27,
));
$set_product = $this->context->smarty->fetch($this->local_path.'views/templates/front/yourtplfile.tpl');
return $set_product;
}
}
yourtplfile.tpl
{url entity='product' id=$product_id}
I'm using the latest version (2) of SDK for php. Below is a snippet of code for assigning a name tag to existing instance:
try {
$result = $ec2->createTags(array(
'Resources' => array($instanceid),
'Tags' => array(
'Name' => 'PWC_cwc'),
));
} catch (Exception $e) {
die("Failed to create tag name: ".$e."<br>");
}
Output:
Failed to create tag name: exception 'Guzzle\Service\Exception\ValidationException' with message 'Validation errors: [Tags][Name][Tag] must be of type object' in /Users/harry/Documents/workspace/BigData/vendor/guzzle/guzzle/src/Guzzle/Service/Command/AbstractCommand.php:394 Stack trace: #0
I'm guessing something is wrong with the way I pass the argument, but just couldn't figure out the correct way of doing this
The API link for createTags method is here: http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Ec2.Ec2Client.html#_createTags
You have to specify the 'Key' and 'Value' of each tag.
$args = array(
'DryRun' => False,
'Resources' => array($resource),
'Tags' => array(
array(
'Key' => 'firstkey',
'Value' => $firstkeyvalue),
array(
'Key' => 'secondkey',
'Value' => $secondkeyvalue),
array(
'Key' => 'thirdkey',
'Value' => $thirdkeyvalue)
));
$response = $ec2->createTags($args);
Try this:
$result = $ec2->createTags(array(
'Resources' => array($instanceid),
'Tags' => array(
'Tag' => array(
'Key' => '<key>',
'Value' => '<value>'
)
)
));
You need to have an array of 'Tag' inside the 'Tags' array.