Upload video to YouTube as unlisted - php

So, I am able to upload a video to YouTube (direct upload) using the PHP client library and set it to private, but is it possible to set it as unlisted?

You must use this code as a child of the XML element of the request:
<yt:accessControl action="list" permission="denied"/>
If you can't add it manually (usually using zend) you may use this code to add the corresponding zend entry:
//Creates an extension to Zend Framework
$element = new Zend_Gdata_App_Extension_Element('yt:accessControl', 'yt', 'http://gdata.youtube.com/schemas/2007', '');
//Adds the corresponding XML child/attribute
$element->extensionAttributes = array(array('namespaceUri' => '', 'name' => 'action', 'value' => 'list'), array('namespaceUri' => '', 'name' => 'permission', 'value' => 'denied'));
//Adds this extension to you video entry where "$myVideo" is your video to be uploaded
$myVideo->extensionElements = array($element);
Hope this helps :D

Do that.. with API ver 2 and ZEND GDATA.
If you look at the content of a $videoEntry you will note a
$_extensionElements and $_extensionArributes.
So looking backwards from the extended class of VideoEntry
you will found the abstract class Zend_Gdata_App_Base
and it has a function setExtensionElements(array).
So only do what others say to create the accesControlElement
and pass it to that function..
And IT WORKS.
$videoEntry = $yt->getFullVideoEntry($id);
if ($videoEntry->getEditLink() !== null) {
echo "<b>Video is editable by current user</b><br />";
$putUrl = $videoEntry->getEditLink()->getHref();
//set video to unlisted
$accessControlElement = new Zend_Gdata_App_Extension_Element(
'yt:accessControl', 'yt', 'http://gdata.youtube.com/schemas/2007', ''
);
$accessControlElement->extensionAttributes = array(
array(
'namespaceUri' => '',
'name' => 'action',
'value' => 'list'
),
array(
'namespaceUri' => '',
'name' => 'permission',
'value' => 'denied'
));
// here is the hidden function
// it´s on a abstract class Zend/Gdata/App/Base/Base.php
// Where ZEND/Gdata/Youtube/VideoEntry.php extends
$videoEntry->setExtensionElements(array($accessControlElement));
$yt->updateEntry($videoEntry, $putUrl);
}else{
echo "<b>EL Video no es editable por este usuario</b><br />";
}

Related

How to Properly use HTML2PDF and Yii-PDF in Yii?

Hello I'm new to yii so currently don't know how to properly use this Html2pdf & yii-pdf extension in yii to get the pdf..
What i actually want.. I have a page called http://localhost/site/Users/Results.. which is showing the list of users..So If i click on user one, it will open a new page called http://localhost/site/Applicant/1
& for user two, it will be
http://localhost/site/Applicant/2
So on these pages there is all the information of users. I want to put a download PDF button on this page, If user clicks on it, He will be able to download all his information in PDF. There can be many users. Everyuser will be able to download all his information in pdf.
I got the html2pdf & yii-PDF .. I got this settings by searching on google, but unable to find proper example to use it according to my requirements above.
config/main.php
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'HTML2PDF' => array(
'librarySourcePath' => 'application.extensions.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
/*'defaultParams' => array( // More info: http://wiki.spipu.net/doku.php?id=html2pdf:en:v4:accueil
'orientation' => 'P', // landscape or portrait orientation
'format' => 'A4', // format A4, A5, ...
'language' => 'en', // language: fr, en, it ...
'unicode' => true, // TRUE means clustering the input text IS unicode (default = true)
'encoding' => 'UTF-8', // charset encoding; Default is UTF-8
'marges' => array(5, 5, 5, 8), // margins by default, in order (left, top, right, bottom)
)*/
)
),
),
Here is my controller.
class UsersController extends Controller
{
public function createPDF(){
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
$html2pdf->Output();
}
}
im totally new to yii, so how to use this extension, I have never used any extension before.. is there any other method to download pdf of each user information. any suggestions or and example of working code.
config/main.php
'ePdf' => array(
'class' => 'ext.yii-pdf.EYiiPdf',
'params' => array(
'HTML2PDF' => array(
'librarySourcePath' => 'application.vendors.html2pdf.*',
'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap
)
),
),
Controller
class UsersController extends Controller
{
public function createPDF(){
$pdffilename = 'test.pdf';
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
ob_clean();
$html2pdf->Output($pdffilename,"I"); // OUTPUT_TO_BROWSER
/* OUTPUT_TO_BROWSER = "I" */
/* OUTPUT_TO_DOWNLOAD = "D" */
/* OUTPUT_TO_FILE = "F" */
/* OUTPUT_TO_STRING = "S" */
}
}

drupal 8 programmatically create node can only succeeded once

I'v created a new module in Drupal 8, it is just a hello world example.
the code just like the following
class FirstController{
public function content(){
return array(
'#type' => 'markup',
'#markup' => t('G\'day.............'),
);
// <------I added the new node code here
}
}
and I added the following code to the content() function to create a node .
but I found that it can only create the node once, and after that no matter how many time I refresh the module page it won't be creating any more new node again.
use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);
// Create node object with attached file.
$node = Node::create([
'type' => 'article',
'title' => 'Druplicon test',
'field_image' => [
'target_id' => $file->id(),
'alt' => 'Hello world',
'title' => 'Goodbye world'
],
]);
$node->save();
any thing I'm doing wrong here?
You forgot about caching :) Your output is just cached, and that's why your code is called only once (to be more precise, not once, but until cache is valid). Take a look here: Render API and here: Cacheability of render arrays.
To disable caching for current page request you may use the following code:
\Drupal::service('page_cache_kill_switch')->trigger();
So, your controller method may look like the following:
public function content() {
// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
/** #var FileInterface $file */
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_RENAME);
// Create node object with attached file.
$node = Node::create([
'type' => 'article',
'title' => 'Druplicon test',
'field_image' => [
'target_id' => $file->id(),
'alt' => 'Hello world',
'title' => 'Goodbye world'
],
]);
$node->save();
\Drupal::service('page_cache_kill_switch')->trigger();
return array(
'#markup' => 'Something ' . rand(),
);
}

How to extend Parsedown to add a class to table tags

I want to write an extension for Parsedown so that I can add a default class to each of the table tags. I've found that I can successfully hack the source code by adding lines to assign attributes in the blockTable function (around line 870):
$Block = array(
'alignments' => $alignments,
'identified' => true,
'element' => array(
'name' => 'table',
'handler' => 'elements',
'attributes' => array(
'class' => 'table',
),
),
);
However, if I try to loosely follow the Change Element Markup extension tutorial I am unsuccessful (perhaps because the table parsing may be an iterative process and the example in the tutorial is a simple string replacement.)
I've tried:
class Extension extends Parsedown
{
protected function blockTable($Line, array $Block = null)
{
$Block = parent::blockTable($Line, array $Block = null);
$Block['table']['attributes']['class'] = 'table';
return $Block;
}
}
but that doesn't work.
I'm not too sure what's wrong with your code, as your code matches mine. I simply added
'attributes' => array(
'class' => 'table table-responsive'
),
to identifyTable, on line 850 so that it became
$Block = array(
'alignments' => $alignments,
'identified' => true,
'element' => array(
'name' => 'table',
'handler' => 'elements',
'attributes' => array(
'class' => 'table table-responsive',
),
),
);
which works just fine for me. But that appears to be the same for you, minus table-responsive.
What version are you using?
I know this is a very old question asked 5 years, 3 month ago, but I came across this answer on a google search, so thought it was worthwhile answering with the code which outputs table class.
class Extension extends Parsedown {
protected function blockTable($Line, ?array $Block = null)
{
$Block = parent::blockTable($Line, $Block);
if(is_null($Block)){ return; }
$Block['element']['attributes']['class'] = 'table table-responsive';
return $Block;
}
I encountered exactly the same problem with the symfony demo application.
Finally it turned out that it wasn't parsedown, because the output was cleaned up by the html-sanitizer.
Allowing the class attribute for tables solved the issue.
For symfony 4 demo app add to config/packages/html_sanitizer.yaml:
html_sanitizer:
#[...]
sanitizers:
default:
# [...]
tags:
table:
allowed_attributes:
- "class"

File upload in module configuration panel, Prestashop

I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.
I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'file',
'label' => $this->l('XML file'),
'name' => 'XMLIMPORT_XML_FILE',
'desc' => $this->l('Select file you wish to import.'),
'required' => true
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'XMLIMPORT_LINES',
'label' => $this->l('Records per file'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?
I tried doing this:
return array(
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);
And after that this:
$form_values = $this->getConfigFormValues(); // returned array from above
foreach (array_keys($form_values) as $key)
Configuration::updateValue($key, Tools::getValue($key));
And later using my own class for XML conversion like this, hoping that it will give me file handle.
$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');
Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).
I need to pass the file I upload to that constructor. I've been struggling for days now.
#edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?
As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.
After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.
Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.
EDIT:
To intercept the saving process, look up the submit button name and make an if statement:
public function getContent() {
if(Tools::isSubmit('submitButtonName')) {
error_log(print_r($_FILES, 1));
}
}
There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).
prestashop handles the image upload with ImageManager class, this class contains more methods which are useful for handling image upload, resize etc.. so its better refer the default homeslider module for the image upload using a module. This module is handling the image upload process in postProcess method with the help of ImageManager class, this class methods will do the all the processes related to upload.

How do I programmatically create a node?

I'm designing a new module in Drupal 8. It's a long-term project that won't be going public for a few months at least, so I'm using it as a way to figure out what's new.
In this module, I want to be able to programmatically create nodes. In Drupal 7, I would do this by creating the object, then calling "node_submit" and "node_save".
These functions no longer exist in Drupal 8. Instead, according to the documentation, "Modules and scripts may programmatically submit nodes using the usual form API pattern." I'm at a loss. What does this mean? I've used Form API to create forms in Drupal 7, but I don't get what the docs are saying here.
What I'm looking to do is programmatically create at least one and possibly multiple new nodes, based on information not taken directly from a user-presented form. I need to be able to:
1) Specify the content type
2) Specify the URL path
3) Set any other necessary variables that would previously have been handled by the now-obsolete node_object_prepare()
4) Commit the new node object
I would prefer to be able to do this in an independent, highly abstracted function not tied to a specific block or form.
So what am I missing?
use Drupal\node\Entity\Node;
$node = Node::create(array(
'type' => 'your_content_type',
'title' => 'your title',
'langcode' => 'en',
'uid' => '1',
'status' => 1,
'field_fields' => array(),
));
$node->save();
RE: deprecated entity create
Here is a short example of usage without the deprecated functions. This is particularly helpful for dynamic creation:
//define entity type and bundle
$entity_type="node";
$bundle="article";
//get definition of target entity type
$entity_def = \Drupal::entityManager()->getDefinition($entity_type);
//load up an array for creation
$new_node=array(
//set title
'title' => 'test node',
//set body
'body' => 'this is a test body, can also be set as an array with "value" and "format" as keys I believe',
//use the entity definition to set the appropriate property for the bundle
$entity_def->get('entity_keys')['bundle']=>$bundle
);
$new_post = \Drupal::entityManager()->getStorage($entity_type)->create($new_node);
$new_post->save();
The Drupal 8 version of devel/devel_generate module has a good example of this.
$edit_node = array(
'nid' => NULL,
'type' => $node_type,
'uid' => $users[array_rand($users)],
'revision' => mt_rand(0, 1),
'status' => TRUE,
'promote' => mt_rand(0, 1),
'created' => REQUEST_TIME - mt_rand(0, $results['time_range']),
'langcode' => devel_generate_get_langcode($results),
);
if ($type->has_title) {
// We should not use the random function if the value is not random
if ($results['title_length'] < 2) {
$edit_node['title'] = devel_create_greeking(1, TRUE);
}
else {
$edit_node['title'] = devel_create_greeking(mt_rand(1, $results['title_length']), TRUE);
}
}
else {
$edit_node['title'] = '';
}
// #todo Remove once comment become field. http://drupal.org/node/731724
if (Drupal::moduleHandler()->moduleExists('comment')) {
$edit_node['comment'] = variable_get('comment_' . $node_type, COMMENT_NODE_OPEN);
}
$node = entity_create('node', $edit_node);
Using formatted text
Using grep with before/after code lines helped me figure out how to add a node with 'full_html'.
Search the Drupal core code with this :
$ cd drupal/core
$ grep -B 5 -A 5 -r entity_create.*node * > /tmp/temp-grep.txt
Then, open up /tmp/temp-grep.txt in a text editor. Poke around there a bit and you'll see this :
--
modules/editor/src/Tests/EditorFileUsageTest.php- $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="invalid-editor-file-uuid-value" />';
modules/editor/src/Tests/EditorFileUsageTest.php- // Test handling of a non-existing UUID.
modules/editor/src/Tests/EditorFileUsageTest.php- $body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';
modules/editor/src/Tests/EditorFileUsageTest.php- // Test editor_entity_insert(): increment.
modules/editor/src/Tests/EditorFileUsageTest.php- $this->createUser();
modules/editor/src/Tests/EditorFileUsageTest.php: $node = entity_create('node', array(
modules/editor/src/Tests/EditorFileUsageTest.php- 'type' => 'page',
modules/editor/src/Tests/EditorFileUsageTest.php- 'title' => 'test',
modules/editor/src/Tests/EditorFileUsageTest.php- 'body' => array(
modules/editor/src/Tests/EditorFileUsageTest.php- 'value' => $body_value,
modules/editor/src/Tests/EditorFileUsageTest.php- 'format' => 'filtered_html',
--
Note how 'body' now becomes an array with a 'value' and a 'format'.
Best way to create node in Drupal 8 via using core services
$node = \Drupal::entityTypeManager()->getStorage('node')->create([
'type' => 'content_type_machine_name',
'field_text' => 'Foo',
'title' => 'Text Title',
]);
$node->save();
Figured it out. For anyone else with this issue, nodes are now treated as entities, and the entity module is now part of core. So my code ended up looking like this:
$new_page_values = array();
$new_page_values['type'] = 'my_content_type';
$new_page_values['title'] = $form_state['values']['page_title'];
$new_page_values['path'] = $new_page_path;
$new_page = entity_create('node', $new_page_values);
$new_page->save();

Categories