Custom Icon & name in yii2 oAuth client widjet - oauth-2.0

I am trying to create yii2 oAuth server I need custom icon & name for my client in view
This is the code where we set view
<?php echo yii\authclient\widgets\AuthChoice::widget([
'baseAuthUrl' => ['/user/sign-in/oauth']
]) ?>

Follow doc: http://www.yiiframework.com/doc-2.0/yii-authclient-widgets-authchoice.html
You can try:
$authAuthChoice->clientLink($client, 'content_of_your_<a>_tag', ['class' => 'class_of_your_<a>_tag'])
My example:
$authAuthChoice->clientLink($client,
'<span class="fa fa-'.$client->getName().'"></span> Sign in with '.$client->getTitle(),
[
'class' => 'btn btn-block btn-social btn-'.$client->getName(),
])
You can see in:
- File: vendor/yiisoft/yii/authclient/widgets/AuthChoice.php
- Line 182: public function clientLink($client, $text = null, array $htmlOptions = [])

Related

Yii2 Multiple Variables to be passed in URL

I want to generate URL's that can handle multiple parameters.
Eg:
www.example.com/product/index?brand=brand-name,
www.example.com/product/index?category=category-name,
I want the url be like :
www.example.com/brand-name,
www.example.com/category-name
Tried some url rules,but it doesn't work.
'rules' => [
[
'pattern' => '<brand:\w+(-\w+)*>/<category:\w+(-\w+)*>',
'route' => 'product/index',
'defaults' => [
'brand' => null,
'category' => null,
]
]
]
This is my reference :
Reference question
To do this you will have to stick to the prefixed version. So the brand param should always be prefixed with brand- and the category always with category-. Otherwise there is no way to tell what is what.
Add the following rules. This will put everything that matches brand-\w+ in the brand argument and pass it to product/index. Same for category.
'<brand:brand-\w+>' => 'product/index',
'<category:category-\w+>' => 'product/index',
To see that it works
public function actionIndex($brand = null, $category = null) {
echo "Brand: $brand<br />";
echo "Category: $category<br />";
echo Url::toRoute(['dev/index', 'brand' => 'brand-name']) . '<br />';
echo Url::toRoute(['dev/index', 'category' => 'category-name']) . '<br />';
}

Wordpress Development: Why am I seeing duplicate urls when I use "wp_get_attachment_image_src( get_post_thumbnail_id()" to get the featured image?

I'm developing a theme on a local development server. But when I try to get the featured image of posts into my home page, I get a 404 error due to the url appearing like this:
http://localhost:8888/crystal/%E2%80%9Dhttp:/localhost:8888/crystal/wp-
content/uploads/2018/04/restaurant-kitchen-inspired-design-ideas.png
See how the domain repeats itself?
I've checked my wp_options table in the database and it looks correct. Both "siteurl" and "home" does have the "http://" already and does not have a "/" after the url. I also already tried adding this script to my wp-config.php:
define('WP_HOME','http://localhost:8888/crystal');
define('WP_SITEURL','http://localhost:8888/crystal');
and this to my functions.php file:
update_option( 'siteurl', 'http://localhost:8888/crystal' );
update_option( 'home', 'http://localhost:8888/crystal' );
but that didn't fix it.
So far, it seems to be only happening when I try to use "wp_get_attachment_image_src( get_post_thumbnail_id()" to get the featured image.
Here is my code:
<?php
$args = array(
'post_type' => 'tournament',
'post_status' => 'publish',
'posts_per_page' => '10'
);
$tournaments_loop = new WP_Query( $args );
if ( $tournaments_loop->have_posts() ) :
while ( $tournaments_loop->have_posts() ) : $tournaments_loop
>the_post();
// Set variables
$title = get_the_title();
$description = get_the_content();
$tournament_date_time = get_field('tournament_date_time');
$location = get_field('location');
$featured_image = wp_get_attachment_image_src(
get_post_thumbnail_id(), 'full' );
$tournament_image = $featured_image[0];
// Output
?>
<div class=”tournament”>
<h3><?php echo $title; ?></h3>
<p><?php echo $tournament_date_time; ?></p>
<p><?php echo $location; ?></p>
<img src=”<?php echo $tournament_image; ?>” alt=”<?php echo
$title; ?>”>
</div>
<?php
endwhile;
wp_reset_postdata();
endif; ?>
Thank you.

How to not escape html entities in a submit button value?

In my form class I am adding a submit button:
$this->add([
'name' => 'submit',
'attributes' => [
'type' => 'submit',
'value' => 'Login ▹',
],
]);
The output is:
<input name="submit" type="submit" value="Login &#9657;">
How do I stop the value from being escaped?
Edit
Based on #RubenButurca answer, here is the output:
It felt odd escaping the value of my submit input. When I tried values such as <i class="fa fa-caret-right"></i> it, well, didn't escape the quotes and I ended up with random attributes in my input element. As such, I've swapped from an input field to a button.
$this->add([
'name' => 'submit',
'type' => 'button',
'attributes' => [
'type' => 'submit',
'required' => 'required',
'value' => 'Login <i class="fa fa-caret-right"></i>',
],
'options' => [
'label_options' => [
'disable_html_escape' => true,
],
],
]);
ZendButon requires a label, which I didn't want. So I extended the view helper to take the label value from the element value ($buttonContent). Because there is no label attribute there is no label echoed, but the escaped value is still shown in the button tags.
FormButton:
use Zend\Form\View\Helper\FormButton as ZendFormButton;
use Zend\Form\ElementInterface;
class FormButton extends ZendFormButton
{
/**
* Invoke helper as functor
*
* Proxies to {#link render()}.
*
* #param ElementInterface|null $element
* #param null|string $buttonContent
* #return string|FormButton
*/
public function __invoke(ElementInterface $element = null, $buttonContent = null)
{
if (!$element) {
return $this;
}
// New code here
if(is_null($buttonContent)) {
$buttonContent = $element->getValue();
}
return $this->render($element, $buttonContent);
}
}
Output:
<button type="submit" name="submit" value="Login <i class="fa fa-caret-right"></i>">Login <i class="fa fa-caret-right"></i></button>
If I understand correctly, you want to display
Login ▹
First of all, your result suggests that you have a function somewhere in the code that replaces the escape characters with their codes.
Second, after you find that piece of code that replaces & with its code, you might have to write "Login &#9657;" in order to obtain "Login ▹" in your HTML code.
Check your code for a function that replaces special characters with their escape codes. It's difficult to figure out without knowing how your code ends-up in the browser, what code is calling it on its turn, etc.
However I am 100% sure you have a function that takes the value and substitutes the special characters with their escape codes - I found your code here: http://www.w3schools.com/charsets/ref_utf_geometric.asp
I have had to extend the FormSubmit view helper:
use Zend\Form\ElementInterface;
use Zend\Form\View\Helper\FormSubmit as ZendFormSubmit;
class FormSubmit extends ZendFormSubmit
{
protected $_options;
/**
* Render a form <input> element from the provided $element
*
* #param ElementInterface $element
* #throws Exception\DomainException
* #return string
*/
public function render(ElementInterface $element)
{
$this->_options = $element->getOptions();
return parent::render($element);
}
/**
* Create a string of all attribute/value pairs
*
* Escapes all attribute values
*
* #param array $attributes
* #return string
*/
public function createAttributesString(array $attributes)
{
$attributes = $this->prepareAttributes($attributes);
$escape = $this->getEscapeHtmlHelper();
$escapeAttr = $this->getEscapeHtmlAttrHelper();
$strings = [];
foreach ($attributes as $key => $value) {
$key = strtolower($key);
if (!$value && isset($this->booleanAttributes[$key])) {
// Skip boolean attributes that expect empty string as false value
if ('' === $this->booleanAttributes[$key]['off']) {
continue;
}
}
//check if attribute is translatable
if (isset($this->translatableAttributes[$key]) && !empty($value)) {
if (($translator = $this->getTranslator()) !== null) {
$value = $translator->translate($value, $this->getTranslatorTextDomain());
}
}
if(array_key_exists('disable_html_escape', $this->_options) &&
array_key_exists($key, $this->_options['disable_html_escape']) &&
$this->_options['disable_html_escape'][$key] === TRUE) {
$strings[] = sprintf('%s="%s"', $escape($key), $value);
continue;
}
//#TODO Escape event attributes like AbstractHtmlElement view helper does in htmlAttribs ??
$strings[] = sprintf('%s="%s"', $escape($key), $escapeAttr($value));
}
return implode(' ', $strings);
}
}
This code saves the element options in a protected variable so it can be accessed in the createAttributesString function. Inside this function, just before the #todo, I check if the escape html option exists, and if set to true then don't escape the attribute value.
Use is:
$this->add([
'name' => 'submit',
'attributes' => [
'type' => 'submit',
'value' => 'Login ▹',
],
'options' => [
'disable_html_escape' => [
'value' => true,
],
],
]);
I am using an array so that I may select, specifically, which attribute to not escape - in this case value.
Rendered output is:
<input type="submit" name="submit" value="Login ▸">

CKEditor / KCfinder Upload (Yii2)

I do not know what happened.
I can't upload files by ckeditor and Kcfinder on Yii2.
Yii2 Advanced form
I get the following warning message:
10.jpg: The uploaded file exceeds 64MB bytes. (screenshot)
File php.ini
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads=On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir="C:\xampp\tmp"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=64MB
; Maximum number of files that can be uploaded via a single request
max_file_uploads=10
post_max_size=64M
File backend\views\information\ _form.php
<?php
namespace backend\modules;
use yii\helpers\ArrayHelper;
use iutbay\yii2kcfinder\KCFinderAsset;
class CKEditor extends \dosamigos\ckeditor\CKEditor
{
public $enableKCFinder = true;
/**
* Registers CKEditor plugin
*/
protected function registerPlugin()
{
if ($this->enableKCFinder)
{
$this->registerKCFinder();
}
parent::registerPlugin();
}
/**
* Registers KCFinder
*/
protected function registerKCFinder()
{
$register = KCFinderAsset::register($this->view);
$kcfinderUrl = $register->baseUrl;
$browseOptions = [
'filebrowserBrowseUrl' => $kcfinderUrl . '/browse.php?opener=ckeditor&type=files',
'filebrowserUploadUrl' => $kcfinderUrl . '/upload.php?opener=ckeditor&type=files',
];
$this->clientOptions = ArrayHelper::merge($browseOptions, $this->clientOptions);
}
}
File backend\modules\ckeditor.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use backend\models\Menulist;
//use dosamigos\ckeditor\CKEditor;
use backend\modules\CKEditor;
use iutbay\yii2kcfinder\KCFinder;
use yii\helpers\Url;
// kcfinder options
// http://kcfinder.sunhater.com/install#dynamic
$kcfOptions = array_merge(KCFinder::$kcfDefaultOptions, [
'uploadURL' => Yii::getAlias('#web').'/upload',
'access' => [
'files' => [
'upload' => true,
'delete' => false,
'copy' => false,
'move' => false,
'rename' => false,
],
'dirs' => [
'create' => true,
'delete' => false,
'rename' => false,
],
],
]);
// Set kcfinder session options
Yii::$app->session->set('KCFINDER', $kcfOptions);
/* #var $this yii\web\View */
/* #var $model backend\models\Information */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="information-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'menulist_id')->dropDownList(
ArrayHelper::map(Menulist::find()->all(),'id','menulist_name'),
['prompt'=>'Select Menulist']
) ?>
<?= $form->field($model, 'information_detail')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'full'
]) ?>
<?= $form->field($model, 'information_status')->dropDownList([ 'active' => 'Active', 'inactive' => 'Inactive', ], ['prompt' => 'Status']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

Yii 2 GridView Link Not Working

I have this code in my index.php in my view:
<p>
<?= Html::a('Create Invoice', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'inv_id',
'cust_name',
'currency',
'inv_date',
'inv_duedate',
'prod_name',
//'prod_desc',
//'prod_quanity',
'prod_price',
//'prod_tax',
//'amount',
//'subtotal',
'total',
[
'attribute' => 'image',
'format' => 'raw',
'value' => function($data){
//return Html::a($data->image, $data->image, $data->image);
return Html::a(Html::encode($data->image),$data->image);
//return Html::a($data->image, $data->image, array('target' => '_blank'));
//return Html::a(Html::encode('file'),'invoice/index');
}
],
//'poso_num',
//'subheading',
//'footer',
//'memo',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I have already displayed the link/path of a specific file, when I click it, nothing happens. When I hover it, I can see the link, example: file:///C:/wamp3/www/basicaccounting/web/pdf/attachment.pdf, in the status bar (lower left corner of the page). I also tried right click + Open in New Tab, the url is just about:blank.
I also tried each of those commented return statements, still the same results.
Any thoughts about this?
Edit:
My problem is with my file path i.e. file:///C:/wamp3/www/basicaccounting/web/pdf/attachment.pdf
My path in the link needs to be relative to the document root i.e. /basicaccounting/web/pdf/attachment.pdf, and not in C drive.
So I tried:
'value' => function($data){
$basepath = str_replace('\\', '/', Yii::$app->basePath).'/web/';
$path = str_replace($basepath, '', $data->file);
return Html::a($data->file, $path, array('target'=>'_blank'));
}
Now it works fine.
I think I have solved my own problem.
My problem is with my file path i.e. file:///C:/wamp3/www/basicaccounting/web/pdf/attachment.pdf
My path in the link needs to be relative to the document root i.e. /basicaccounting/web/pdf/attachment.pdf, and not in C drive.
So I tried:
'value' => function($data){
$basepath = str_replace('\\', '/', Yii::$app->basePath).'/web/';
$path = str_replace($basepath, '', $data->file);
return Html::a($data->file, $path, array('target'=>'_blank'));
Now it works fine. Thanks everyone!

Resources