Default value for select in custom Modifier - magento

I have a select field from a form defined like bellow. As you can see that select has two values, Yes and No. I'm looking to set a default value for this select.
"children" => [
"bc_offer_is_duration_count_fixed" => [
"arguments" => [
"data" => [
"config" => [
"dataType" => "select",
"formElement" => "select",
"visible" => "1",
"required" => "1",
"validation" => [
'required-entry' => "1"
],
"default" => null,
"label" => __('Is duration count fixed'),
"scopeLabel" => __('[GLOBAL]'),
"code" => "bc_offer_offer_durations",
"source" => "content",
"globalScope" => true,
"sortOrder" => 10,
"componentType" => "field",
"component" => "Project_OfferProducts/js/form/element/offer-is-duration-count-fixed",
'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']]
]
]
]
]
]
It's part of the modifyData method from a class defined here
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier as CatalogAbstractModifier;
abstract class AbstractModifier extends CatalogAbstractModifier
Obviously I already tried to set "default" => "Yes" and "default" => "1" and "default" => 1
I also have an offer-js-duration-count-fixed.js file with that content
define([
'Magento_Ui/js/form/element/select',
'Project_OfferProducts/js/model/offer-configuration/context'
], function (Select, context) {
'use strict';
return Select.extend({
updatingDurationFromField: false,
initialize: function () {
this._super();
context.isDurationCountFixed.subscribe(function(newValue){
this.refreshInField(newValue);
}.bind(this));
this.value.subscribe(this.refreshInGrid.bind(this));
if(context.isDurationCountFixed())
{
this.value(1);
}
else
{
this.value(0);
}
return this;
},
refreshInGrid: function(){
this.updatingDurationFromField = true;
context.isDurationCountFixed(this.value());
this.updatingDurationFromField = false;
},
refreshInField: function(newValue){
if(!this.updatingDurationFromField)
{
this.value(context.isDurationCountFixed());
}
}
});});
I'm under Magento 2.

just what i think
change this
'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']]
to
'options' => [['label' => __('Yes'), 'value' => 1], ['label' => __('No'), 'value' => 0]]
and change
"default" => null,
to
"default" => "1",

Related

How Get Index If Object In Array Contains Desired Value in Ruby

So this is an array I get from somewhere and I wanted to get the index of the object that contains "text" => "Assets". How can i do that in Ruby?
Example
[{
"class" => "android.support.v7.widget.AppCompatTextView",
"tag" => nil,
"description" => "android.support.v7.widget.AppCompatTextView{b777d8c V.ED..... ........ 0,39-355,168 #7f0e007f app:id/textview_sectiontitle}",
"id" => "textview_sectiontitle",
"text" => "Assets",
"visible" => true,
"rect" => {
"height" => 129,
"width" => 355,
"y" => 2088,
"x" => 80,
"center_x" => 257,
"center_y" => 2152
},
"enabled" => true,
"contentDescription" => nil
},
{
"class" => "android.support.v7.widget.AppCompatImageButton",
"tag" => nil,
"description" => "android.support.v7.widget.AppCompatImageButton{ae57704 VFED..C.. ........ 1056,0-1280,208 #7f0e0109 app:id/imagebutton_amount}",
"id" => "imagebutton_amount",
"visible" => true,
"rect" => {
"height" => 208,
"width" => 224,
"y" => 2049,
"x" => 1136,
"center_x" => 1248,
"center_y" => 2153
},
"enabled" => true,
"contentDescription" => nil
}]
Try this one
arr.index { |item| item["text"] == "Assets" }

How to add OR condition in Elasticsearch ruby query?

In my use case, I would like to add an OR condition in Elasticsearch query. Here is my query,
query_body = {
'query' => {
'bool' => {
'must' => [{ 'range' => {'#timestamp' => { 'from' => stream_filters[:first_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ"), 'to' => stream_filters[:second_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ") } } }, {'term' => {"#timeout" => true} }, {'term' => {"#dest" => dest} }, {'term' => {"#source" => source} } ]
}
}, 'facets' => facets
}
I would like to add 'term' => {"#dest" => ' '} empty check for #dest along with 'term' => {"#dest" => dest}
I tried to add an or condition, But it is not working.
query_body = {
'query' => {
'bool' => {
'must' => [{ 'range' => {'#timestamp' => { 'from' => stream_filters[:first_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ"), 'to' => stream_filters[:second_time].gmtime.strftime("%Y-%m-%dT%H:%M:%SZ") } } }, {'term' => {"#timeout" => true} }, {'term' => {"#source" => source} } ],
'filter' => {
'or' => [{
'term' => { "#dest" => dest }
'term' => { "#dest" => ' ' }
}]
}
}
}, 'facets' => facets
}
Could someone help me with this?
It seems like a syntax error in your filter clause. Please try with the correct syntax as below :
'filter' => {
'or' => [
{
'term' => { "#dest" => dest }
},
{
'term' => { "#dest" => ' ' }
}
]
}

How to always show single validation message in ZF2 validators?

I have the following input:
private function addBirthdayElement()
{
return $this->add(
array(
'type' => 'DateSelect',
'name' => 'x_bdate',
'options' => [
'label' => 'astropay_birthday',
'label_attributes' => array(
'class' => 'astropay-label'
),
'create_empty_option' => true,
'render_delimiters' => false,
],
'attributes' => array(
'required' => true,
'class' => 'astropay-input',
)
)
);
}
It has the following filter:
public function addBirthdayFilter()
{
$time = new \DateTime('now');
$eighteenYearAgo = $time->modify(sprintf('-%d year', self::EIGHTEEN_YEARS))->format('Y-m-d');
$this->add(
[
'name' => 'x_bdate',
'required' => true,
'validators' => [
[
'name' => 'Between',
'break_chain_on_failure' => true,
'options' => [
'min' => 1900,
'max' => $eighteenYearAgo,
'messages' => [
Between::NOT_BETWEEN => 'astropay_invalid_birth_date_18',
Between::NOT_BETWEEN_STRICT => 'astropay_invalid_birth_date_18',
]
]
],
[
'name' => 'Date',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
],
],
]
);
return $this;
}
However, putting an empty date, I get the error message defined for:
Date::INVALID_DATE
But it's not the overridden one. The break_chain_on_failure works for the two validators I have defined, but the default Zend message is always there. For example I get this as an error in my form:
The input does not appear to be a valid date
astropay_invalid_birth_date_18
How can I display only the overidden error messages and 1 at a time?
You can use a message key in your validator configuration instead of a messages array to always show a single message per validator.
For example, replace this:
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
with this one:
'options' => [
'message' => 'Invalid birth date given!',
]

What's the best way to replace a string inside a string in ruby?

I have a bunch of these:
'link' => "http://twitter.com/home?status=Check out "{title}" {url}",
And Want to replace the {title} and {url} bits.
I'm currently doing this with gsub:
l.gsub! "{url}", URI::encode(#opts[:url])
l.gsub! "{title}", URI::encode(#opts[:title])
But I have the feeling there's a much better way to do this than with gsub...
#
This is an edit / addition to clarify:
class SocialBookmarkMaker
require 'open-uri'
attr_accessor :opts
def initialize(opts)
#opts = ##default_opts.merge opts
end
##default_opts = {
:icon_folder => "/images/icons/social_aquatic/24 X 24",
:sites => ['facebook', 'twitter', 'delicious', 'digg', 'stumbleupon', 'reddit', 'technorati', ],
:ext => 'png',
:url => 'not provided',
:title => 'not provided',
}
##bookmarks = {
'yahoo' => {
'name' => 'Yahoo! My Web',
'link' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u={url}&t={title}',
},
'google' => {
'name' => 'Google Bookmarks',
'link' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk={url}&title={title}',
},
'windows' => {
'name' => 'Windows Live',
'link' => 'https://favorites.live.com/quickadd.aspx?url={url}&title={title}',
},
'facebook' => {
'name' => 'Facebook',
'link' => 'http://www.facebook.com/sharer.php?u={url}&t={title}',
},
'digg' => {
'name' => 'Digg',
'link' => 'http://digg.com/submit?phase=2&url={url}&title={title}',
},
'ask' => {
'name' => 'Ask',
'link' => 'http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url={url}&title={title}',
},
'technorati' => {
'name' => 'Technorati',
'link' => 'http://www.technorati.com/faves?add={url}',
},
'delicious' => {
'name' => 'del.icio.us',
'link' => 'http://del.icio.us/post?url={url}&title={title}',
},
'stumbleupon' => {
'name' => 'StumbleUpon',
'link' => 'http://www.stumbleupon.com/submit?url={url}&title={title}',
},
'squidoo' => {
'name' => 'Squidoo',
'link' => 'http://www.squidoo.com/lensmaster/bookmark?{url}'
},
'netscape' => {
'name' => 'Netscape',
'link' => 'http://www.netscape.com/submit/?U={url}&T={title}',
},
'slashdot' => {
'name' => 'Slashdot',
'link' => 'http://slashdot.org/bookmark.pl?url={url}&title={title}',
},
'reddit' => {
'name' => 'reddit',
'link' => 'http://reddit.com/submit?url={url}&title={title}',
},
'furl' => {
'name' => 'Furl',
'link' => 'http://furl.net/storeIt.jsp?u={url}&t={title}',
},
'blinklist' => {
'name' => 'BlinkList',
'link' => 'http://blinklist.com/index.php?Action=Blink/addblink.php&Url={url}&Title={title}',
},
'dzone' => {
'name' => 'dzone',
'link' => 'http://www.dzone.com/links/add.html?url={url}&title={title}',
},
'swik' => {
'name' => 'SWiK',
'link' => 'http://stories.swik.net/?submitUrl&url={url}'
},
'shoutwire' => {
'name' => 'Shoutwrie',
'link' => 'http://www.shoutwire.com/?p=submit&&link={url}',
},
'blinkbits' => {
'name' => 'Blinkbits',
'link' => 'http://www.blinkbits.com/bookmarklets/save.php?v=1&source_url={url}',
},
'spurl' => {
'name' => 'Spurl',
'link' => 'http://www.spurl.net/spurl.php?url={url}&title={title}',
},
'diigo' => {
'name' => 'Diigo',
'link' => 'http://www.diigo.com/post?url={url}&title={title}',
},
'tailrank' => {
'name' => 'Tailrank',
'link' => 'http://tailrank.com/share/?link_href={url}&title={title}',
},
'rawsugar' => {
'name' => 'Rawsugar',
'link' => 'http://www.rawsugar.com/tagger/?turl={url}&tttl={title}&editorInitialized=1',
},
'twitter' => {
'name' => 'Twitter',
'link' => "http://twitter.com/home?status=Check out "{title}" {url}",
},
}
def self.bookmarks
##bookmarks
end
def icon_loc(site)
"http://common-resources.---.net.s3.amazonaws.com#{#opts[:icon_folder]}/#{site}.#{#opts[:ext]}"
end
def link_url(site)
l = SocialBookmarkMaker.bookmarks[site]['link']
l.gsub! "{url}", URI::encode(#opts[:url])
l.gsub! "{title}", URI::encode(#opts[:title])
l
end
end
shared/social_bookmarks/standard.html.haml
- opts ||= {}
- opts.merge! :url => request.url
- opts.merge! :title => "---.net: #{#layout[:social_bookmark_title] || #layout[:title] || default_view_title}"
- b = SocialBookmarkMaker.new opts
- b.opts[:sites].each do |site|
= link_to(image_tag( b.icon_loc(site) ), b.link_url(site), :title => "Share on #{SocialBookmarkMaker.bookmarks[site]['name']}")
I then call this like this in my rails layout:
render :partial => "shared/social_bookmarks/standard", :locals => { :opts => {:icon_folder => "/images/icons/social_aquatic/48 X 48" }}
Either you change your string to look like
"http://twitter.com/home?status=Check out "%{title}" %{url}"
and then use printf with a Hash
s = "http://twitter.com/home?status=Check out "%{title}" %{url}"
# you can of course use #opts as the Hash here.
s = s % {:title => "abc", :url => "def"} # => "http://twitter.com/home?status=Check out "abc" def"
and accept that it only works with Ruby 1.9.2 and upwards, or you continue using gsub but using the block syntax to condense it:
s.gsub!(/\{(.+?)\}/) do |m|
#opts[$1.to_sym]
end
You can just embed the variables directly in the string
'link' => "http://twitter.com/home?status=Check out "#{title}" #{url}"

Recurrence with Google calendar ruby API V3

Using the following code, I've not been able to push any recurring events to google calendar. However, take off the 'recurrence' item from event and it works.
What am I doing wrong ?
event = {
'summary' => 'Appointment',
'location' => 'Somewhere',
'start' => {
'dateTime' => '2011-06-03T10:00:00.000-07:00'
},
'end' => {
'dateTime' => '2011-06-03T10:25:00.000-07:00'
},
'recurrence' => [
"RRULE:FREQ=DAILY;COUNT=5"
]
}
result = #client.execute(:api_method => #service.events.insert,
:parameters => {'calendarId' => 'hg9a7o16bm6dj0tmuo481499mc#group.calendar.google.com'},
:body_object => event,
:headers => {'Content-Type' => 'application/json'})
puts result.data.id.to_s
The timezone needs to be set in a seperate field as this
event = {
'summary' => 'Appointment',
'location' => 'Somewhere',
'start' => {
'dateTime' => '2011-06-03T10:00:00.000-07:00',
'timeZone' => 'America/Montreal'
},
'end' => {
'dateTime' => '2011-06-03T10:25:00.000-07:00',
'timeZone' => 'America/Montreal'
},
'recurrence' => [
"RRULE:FREQ=DAILY;COUNT=5"
]
}
Please try with "RRULE:FREQ=WEEKLY;UNTIL=20120701T160000Z";

Resources