I need to set values for default address fields(langcode, country_code, administrative_area, address_locality ect.) when I create a node. I used below code in the submitForm function of a Form class which is extends by Drupal\Core\Form\FormBase class. But it not works for me.
$venueNode = Node::create([
'type' => 'venue',
'title' => 'Venue',
'field_address' => [
'country_code' => 'US',
'address_line1' => '1098 Alta Ave',
'locality' => 'Mountain View',
'administrative_area' => 'US-CA',
'postal_code' => '94043',
],
]);
$venueNode->save();
I made a mistake here. There should be a 0 index for field_address. Therefore the code should be like below.
$venueNode = Node::create([
'type' => 'venue',
'title' => 'Venue',
'field_address' => [
0 => [
'country_code' => 'US',
'address_line1' => '1098 Alta Ave',
'locality' => 'Mountain View',
'administrative_area' => 'US-CA',
'postal_code' => '94043',
],
],
]);
$venueNode->save();
Related
I'm trying to migrate multiple migration tables into multiple databases I'm getting this kind of error:
// If the configuration doesn't exist, we'll throw an exception and bail. 149| $connections = $this->app['config']['database.connections'];
150|
151| if (is_null($config = Arr::get($connections, $name))) { >
152| throw new InvalidArgumentException("Database [{$name}] not configured."); 153| }
154|
155| return (new ConfigurationUrlParser)
156| ->parseConfiguration($config); Exception trace: 1 Illuminate\Database\DatabaseManager::configuration("s_request") /home/dipu/A1pathshala/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:115 2 Illuminate\Database\DatabaseManager::makeConnection("s_request") /home/dipu/A1pathshala/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:86 Please use the argument -v to see more details.
here's my configuration:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=A1pathshala
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=school_request
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=
database.php
connection=>[
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
//secondary database
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
In migration table use this.
public function up()
{
Schema::connection('mysql2')->create('schools', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('mysql2')->dropIfExists('schools');
}
}
and migrating use this
php artisan migrate --database=mysql2
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this->config('category_dynamic_block.settings');
$form['section_title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Section Title'),
'#description' => $this->t('Enter a Section Title'),
);
$form['term_name'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => array('categories'),
],
'#title' => $this->t('Term Name'),
'#description' => $this->t('Enter a Category Vocab Term Name'),
);
$form['page_title'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#selection_settings' => [
'target_bundles' => array('article'),
],
'#title' => $this->t('Page Title (' . $i . ')'),
'#description' => $this->t('Enter Page Title to be displayed'),
);
return $form;}
I'm creating a configuration form and I'm trying to find if there is a way in drupal 8 which will allow the user to enter multiple values for $form['page_title'] field.
This question (unlimited text fields with form api) may be what you are looking for: https://drupal.stackexchange.com/questions/208012/unlimited-textfields-with-form-api
Basically you'll need to add some ajax:
'#ajax' => [
'callback' => array($this, 'addMultipleItems'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Adding another item...'),
),
],
I am trying use Doctrine within Slim to connect to a DB2 db. I am getting no errors. But, my app is not connecting to the database. I am using (likely incorrectly) this package for the driver: alanseiden/doctrine-dbal-ibmi
Here is the pertinent bit from my DIC:
// Doctrine
$container['db'] = function ($c) {
$settings = $c->get('settings');
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
$settings['doctrine']['meta']['entity_path'],
$settings['doctrine']['meta']['auto_generate_proxies'],
$settings['doctrine']['meta']['proxy_dir'],
$settings['doctrine']['meta']['cache'],
false
);
return \Doctrine\ORM\EntityManager::create($settings['doctrine']['connection'], $config);
};
And, the referenced settings:
// doctrine settings
'doctrine' => [
'meta' => [
'entity_path' => [
'app/src/Entity'
],
'auto_generate_proxies' => true,
'proxy_dir' => __DIR__.'/../cache/proxies',
'cache' => null,
],
'connection' => [
'Description' => 'IBM i Access ODBC Driver 64-bit',
'driver' => '\DoctrineDbalIbmi\Driver\DB2Driver::class',
'System' => 'xx.xx.xx.xx',
'UserID' => '*****',
'Password' => '*****',
'Naming' => 0,
'DefaultLibraries' => 'QGPL',
'ConnectionType' => 0,
'CommitMode' => 2,
'ExtendedDynamic' => 1,
'DefaultPkgLibrary' => 'QGPL',
'DefaultPackage' => 'A/DEFAULT(IBM),2,0,1,0,512',
'AllowDataCompression' => 1,
'MaxFieldLength' => 32,
'BlockFetch' => 1,
'BlockSizeKB' => 128,
'ExtendedColInfo' => 0,
'LibraryView' => 'ENU',
'AllowUnsupportedChar' => 0,
'ForceTranslation' => 0,
'Trace' => 0
]
]
],
];
I appreciate any help or direction.
to populate my form I use the fieldset approach. For one given form field I will use a select and the options are coming directly from an entity like this:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'city',
'options' => array(
'label' => 'City: ',
'object_manager' => $this->_om,
'target_class' => 'Hotbed\Entity\AllAdresses',
'property' => 'city',
'is_method' => true,
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('postal_code' => $postalCode),
'orderBy' => array('city' => 'ASC'),
),
),
),
'attributes' => array(
'class' => 'form-control input-large',
'required' => '*'
),
)
);
This works pretty well. The only inconvient is that I have to put a distinct on the field city. How can I solve this problem?
Regards Andrea
The way I got around this was to create a function in the repository to return the distinct entities, and then specify that function name in your form element.
So in your case, for example:
In your repository:
public function findDistinctCitiesByPostalCode($postalCode) {
$dql = "SELECT DISTINCT a.city "
. "FROM Hotbed\Entity\AllAdresses a "
. "WHERE a.postalCode :postalCode";
$qry = $this->getEntityManager()->createQuery($dql);
$qry->setParameter('postalCode', $postalCode);
$results = $qry->getArrayResult();
// $results will be an array in the format
// array(array('city' => 'city_1'), array('city' => 'city_1'),....)
// so you'll need to loop through and create an array of entities
foreach ($results as $row) {
$addresses[] = new Hotbed\Entity\AllAdresses(array('city' => $row['city']);
}
return $addresses;
}
And then in your form:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'city',
'options' => array(
'label' => 'City: ',
'object_manager' => $this->_om,
'target_class' => 'Hotbed\Entity\AllAdresses',
'property' => 'city',
'is_method' => true,
'find_method' => array(
'name' => 'findDistinctCitiesByPostalCode'
)
)
)
);
I need some advice on how to set up a unit test in Cake 2.3 that tests OAuth login. I'm using the thomseddon/cakephp-oauth-server plugin. Note: I've reviewed examples such as CakePHP 2.3 - Unit testing User Login, but I'm still confused about how exactly to approach an OAuth test using the plugin. Any help appreciated.
The following is what I currently have in my unit test. Not very much of a test, yet.
/**
* testOAuthLogin method
* Tests that OAuth login works
* #return void
*/
public function testOAuthLogin(){
$data = array(
'response_type' => 'code',
'client_id' => getenv('THREE_SCALE_APP_ID'),
'User' => array(
'username' => TEST_USERNAME,
'passwd' => TEST_PASSWORD
)
);
$result = $this->testAction('/oauth/login', array(
'data' => $data,
'method' => 'post'
));
debug($result);
}
This returns:
{"error":"invalid_client","error_description":"No client id supplied"}
I was able to figure this out. I just needed to setup up proper fixtures for User and AccessToken. And then I had to ensure that these were imported in the controller that I was testing in via $fixtures.
Example of my AccessTokenFixture:
<?php
App::uses('OAuthComponent', 'OAuth.Controller/Component');
/**
* AccessTokenFixture
*
*/
class AccessTokenFixture extends CakeTestFixture {
/**
* Fields
*
* #var array
*/
public $fields = array(
'oauth_token' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 40, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'client_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'user_id' => array('type' => 'integer', 'null' => false, 'default' => null),
'expires' => array('type' => 'integer', 'null' => false, 'default' => null),
'scope' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'indexes' => array(
'PRIMARY' => array('column' => 'oauth_token', 'unique' => 1)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
);
/**
* init method
* #return void
*/
public function init() {
$this->records = array(
array(
'oauth_token' => OAuthComponent::hash('SAMPLE_ACCESS_TOKEN'),
'client_id' => 'YOUR_CLIENT_ID',
'user_id' => 1,
'expires' => 1367263611232323,
'scope' => ''
),
array(
'oauth_token' => OAuthComponent::hash('SAMPLE_ACCESS_TOKEN'),
'client_id' => 'YOUR_CLIENT_ID',
'user_id' => 2,
'expires' => 13672640632323323,
'scope' => ''
)
);
parent::init();
}
}