Customising Laravel 5.5 Api Resource Collection pagination - laravel-5.5

I have been working with laravel api resource. By default laravel provides links and meta as shown below.
"links": {
"first": "https://demo.test/api/v2/taxes?page=1",
"last": "https://demo.test/api/v2/taxes?page=4",
"prev": null,
"next": "https://demo.test/api/v2/taxes?page=2"
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 4,
"path": "https://demo.test/api/v2/taxes",
"per_page": 2,
"to": 2,
"total": 8
}
But I don't want this, insted i want something like
"pagination": {
"total": 8,
"count": 8,
"per_page": 25,
"current_page": 1,
"total_pages": 1
}
I'm able to get this info but if I do return TaxResource::collection($taxes);, I won't get this. Even I have custom collection method
public static function collection($resource)
{
$resource->pagination = [
'total' => $resource->total(),
'count' => $resource->count(),
'per_page' => $resource->perPage(),
'current_page' => $resource->currentPage(),
'total_pages' => $resource->lastPage()
];
return parent::collection($resource);
}
It is not giving what I want. But if I reference through (TaxResource::collection($taxes))->pagination; I'm able to get that. But I want it to be returned when I do return TaxResource::collection($taxes);

I was interested in your question and spent some time resolving it. I guess there are a lot of work to be done to improve Eloquent: API Resources' functionality in the future.
In order to resolve it I must use Resource Collections instead of Resources:
However, if you need to customize the meta data returned with the collection, it will be necessary to define a resource collection
php artisan make:resource Tax --collection
or
php artisan make:resource TaxCollection
Route:
Route::get('/get-taxes', function () {
$taxes = Taxes::paginate();
return new TaxCollection($taxes);
});
TaxCollection.php:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class TaxCollection extends ResourceCollection
{
public function toArray($request)
{
return [
'data' => $this->collection,
'pagination' => [
'total' => $this->total(),
'count' => $this->count(),
'per_page' => $this->perPage(),
'current_page' => $this->currentPage(),
'total_pages' => $this->lastPage()
],
];
}
// Using Laravel < 5.6
public function withResponse($request, $response)
{
$originalContent = $response->getOriginalContent();
unset($originalContent['links'],$originalContent['meta']);
$response->setData($originalContent);
}
// Using Laravel >= 5.6
public function withResponse($request, $response)
{
$jsonResponse = json_decode($response->getContent(), true);
unset($jsonResponse['links'],$jsonResponse['meta']);
$response->setContent(json_encode($jsonResponse));
}
}
This solve the problem but now there are new one:
Unlike Resources I don't know how to modify toArray fields in Resource Collections, the manual shows only example with 'data' => $this->collection where we send not modified collection (Resource Collections allows us change meta data). So If we use just Resource then we can modify collection data but not meta data.

The accepted answer did not work for me (in Laravel 5.6), but I found a better way IMHO.
Save the pagination informations in your ResourceCollection constructor and replace the Paginator resource with the underlying Collection.
TaxCollection.php:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class TaxCollection extends ResourceCollection
{
private $pagination;
public function __construct($resource)
{
$this->pagination = [
'total' => $resource->total(),
'count' => $resource->count(),
'per_page' => $resource->perPage(),
'current_page' => $resource->currentPage(),
'total_pages' => $resource->lastPage()
];
$resource = $resource->getCollection();
parent::__construct($resource);
}
public function toArray($request)
{
return [
'data' => $this->collection,
'pagination' => $this->pagination
];
}
}

So I've discovered that in PHP you can actually call a grandparent function without reflection or other workarounds.
Given that TaxCollection extends ResoureCollection, which in turn extends JsonResource we can actually bypass the ResourceCollection method that handles the pagination.
class TaxCollection extends ResourceCollection
{
public function toArray($request)
{
return [
'data' => $this->collection,
'pagination' => [
'total' => $this->total(),
'count' => $this->count(),
'per_page' => $this->perPage(),
'current_page' => $this->currentPage(),
'total_pages' => $this->lastPage()
],
];
}
public function toResponse($request)
{
return JsonResource::toResponse($request);
}
}
the toResponse method call is NOT static, but instead calling the grandparent JsonResource::toResponse method, just as parent::toResponse would call the ResourceCollection toResponse(..) instance method.
This will remove all extra pagination fields from the JSON response (links, meta, etc) and allow you to customize the response as you'd like in toArray($request)

you could also extends JsonResource, AnonymousResourceCollection, ResourceCollection and finally PaginatedResourceResponse

#yrv16 Laravel 5.6 version:
public function withResponse($request, $response)
{
$jsonResponse = json_decode($response->getContent(), true);
unset($jsonResponse['links'],$jsonResponse['meta']);
$response->setContent(json_encode($jsonResponse));
}

JsonResource class comes with an additional() method which lets you specify any additional data you’d like to be part of the response when working with a resource:
Route::get('/get-taxes', function () {
$taxes = Taxes::paginate();
return new TaxCollection($s)->additional([
'pagination' => [
'total' => $taxes->total,
...
]
]);
});

Related

Create custom module for render custom forms through a controller in Drupal 8

I need to render a custom form which is created using Drupal\Core\Form\FormBase and Drupal\Core\Form\FormStateInterface through a controller in custom Drupal 8 module. Is there any guidence or reference to follow to do this?
Actually I tried to render form directly and through a controller. But both ways are not working. Only render the submit button. I refer the drupal 8 documentation also. But I couldn't find a solution for this. Please be kind enough to find my coding samples below. If there are anything wrong. Please correct me.
my_module.routing.yml
partner.content:
path: '/partner'
defaults:
_controller: '\Drupal\partner\Controller\PartnerController::add'
_title: 'Add Partner'
requirements:
_permission: 'access content'
partner.addform:
path: '/partner/add'
defaults:
_form: '\Drupal\partner\Form\AddForm'
_title: 'Add Partner'
requirements:
_permission: 'access content'
AddForm.php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class AddForm extends FormBase
{
/**
* Returns form id
*
* #return string
*/
public function getFormId(): string
{
return 'my_module_add_form';
}
/**
* Build form array
*
* #param array $form
* #param FormStateInterface $formState
* #return array
*/
public function buildForm(array $form, FormStateInterface $form_state): array
{
// First name
$form['first_name'] = [
'#type' => 'textField',
'#title' => t('First Name'),
'#required' => true,
];
// Other input fields...
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save Changes'),
'#button_type' => 'primary',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {}
public function submitForm(array &$form, FormStateInterface $form_state) {}
}
MyModuleController.php
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\my_module\Form\AddForm;
class MyModuleController extends ControllerBase
{
public function add()
{
$addForm = new AddForm();
$form = \Drupal::formBuilder()->getForm($addForm);
return [
'#theme' => 'form_my_module_add',
'#form' => $form,
];
}
}
Happy to find out the solution with Hemantha Dhanushka on my comment.
To make it clear this question has a correct answer, here I past the validated comment.
I would recommend you to use the first approach (using routing::_form instead
of Controller). Also, it seems you use the wrong #type for your
first_name field. Try textfield instead of textField.
Also, for people who want to go further, here are some links to implement a proper
routing::_form approach to expose a form as a page instead of using a Controller: https://www.valuebound.com/resources/blog/step-by-step-method-to-create-a-custom-form-in-drupal-8.
For people looking for more help about existing Form Element Reference (textfield, checkboxes, entity_autocomplete, ...) here is an excellent up-to-date article https://drupalize.me/tutorial/form-element-reference?p=2766
You can use buildForm() method for it. Check below code example:
public function add()
{
$form_state = new Drupal\Core\Form\FormState();
$form_state->setRebuild();
$form = \Drupal::formBuilder()->buildForm('Drupal\my_module\Form\AddForm', $form_state);
return [
'#theme' => 'form_my_module_add',
'#form' => $form,
];
}
Reference: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormBuilder.php/function/FormBuilder::getForm/8.2.x

Retrieve a taxonomy term in the buildrow function of a drupal 8 custom entity

I have built a custom entity that works well. One of my fields is a taxonomy but I can not retrieve the name of the term in the buildRow(EntityInterface $entity) function which displays my records.
For a simple string field I do: $row['foo'] = $entity->foo->value;
How to do a taxonomy term that is an entity_reference: $row['bar'] = $entity->BAR_TERM_NAME;
Thank you for your help.
To work as requested you need 3 things:
Implements an entity_reference field in your custom Entity.
Add a getter methode for you field.
Retrieve your field in your custom ListBuilder -> buildRow().
Check the Drupal 8 documentation about FieldTypes, FieldWidgets and FieldFormatters.
Implements an entity_reference field
Your field foo in your Entity should be generated using the entity_reference field type.
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// Some code ...
$fields['foo'] = BaseFieldDefinition::create('entity_reference')
->setLabel($this->t('Foo field'))
->setDescription($this->t('The Foo field.'))
->setSetting('target_type', 'taxonomy_term')
->setSetting('handler', 'default')
->setSetting('handler_settings', ['target_bundles' => ['vocabulary_id' => 'vocabulary_id']])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'vocabulary_id',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 40,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
// Some code ...
}
You should then replace the 3 vocabulary_id by the vocabulary that you wanna links.
Add a getter
In the same Class as your baseFieldDefinitions.
// Some code ...
public function getFoo() {
return $this->get('foo')->value;
}
// Some code ...
Retrieve the field
In your ListBuilder Class.
public function buildRow(EntityInterface $entity) {
// Some code ...
$row['foo'] = $entity->getFoo();
// Some code ...
}
Hopes it will help you !

Yii2 + Codeception: How to use fixtures?

I wrote a simple test for my Yii2 application using Codeception. Instead of using the real MySQL db, I want to use fixtures.
Here is the code:
tests/PersonTest.php:
namespace app\tests\unit\models;
use tests\fixtures;
use app\controllers;
class PersonTest extends \Codeception\Test\Unit
{
protected $tester;
public $appConfig = '#app/config/main.php';
protected function _before(){ }
protected function _after(){ }
public function _fixtures()
{
return [ 'Person' => fixtures\PersonFixture::className() ];
}
public function testUser(){
$person = Person::findOne( [ "id" => 1 ] );
$userId = isset( $person->id ) ? $person->id : false;
$this->assertEquals( 1, $userId );
}
}
tests/fixtures/data/Person.php
return [
'person1' => [
'id' => 1,
'firstname' => 'Foo',
'lastname' => 'Bar',
],
];
tests/fixtures/Person.php
namespace tests\fixtures;
use yii\test\ActiveFixture;
class PersonFixture extends ActiveFixture
{
public $modelClass = 'app\models\Person';
}
When I run the test, I just get the error:
[Error] Class 'tests\fixtures\PersonFixture' not found
I tried 100 different things, but I can not make it work. If this simple example would work for me, I could create real tests.
With Codeception 2.3.8 you can do it like this:
Define your fixture (and have the data file just like you have in your question)
namespace app\tests\fixtures;
class PersonFixture extends \yii\test\ActiveFixture {
public $modelClass = 'app\models\Person';
}
And write your test
namespace app\tests\unit;
class PersonTest extends \Codeception\Test\Unit {
public function _fixtures() {
return [
'persons' => 'app\tests\fixtures\PersonFixture',
];
}
public function testUser() {
$person1 = $this->tester->grabFixture('persons', 'person1');
$this->assertEquals(1, $person1->id);
}
}
That's it.
with codeception 4.0.3 you can run your fixture by following the steps...
create fixtures folder inside test
[fixture folder][1]
[1]: https://i.stack.imgur.com/uK9Cy.png
inside your fixtures/data/book.php
<?php
return [
'book1' => [
'title' => 'lmayert',
'isbn' => 'Ibn-098',
],
'user2' => [
'title' => 'napoleon69',
'isbn' => 'Ibn-042',
],
];
your fixtures/BookFixture be like this:
<?php
namespace app\tests\fixtures;
use yii\test\ActiveFixture;
/**
*
*/
class BookFixture extends ActiveFixture
{
public $modelClass = 'app\models\Book';
}
Now the tests/unit/BookTest be like this
<?php
use app\tests\unit\fixtures\BookFixture;
class BookTest extends \Codeception\Test\Unit
{
/**
* #var \UnitTester
*/
protected $tester;
protected function _before()
{
}
protected function _after()
{
}
public function _fixtures() {
return [
'books' => 'app\tests\fixtures\BookFixture',
];
}
// tests
public function testBook()
{
$book1 = $this->tester->grabFixture('books','book1');
$this->assertEquals(1,$book1->id);
}
}
I hope this will help
You have to change fixture file name from Person.php to PersonFixture.php and it will start working.
You have to be using yii2-codeception extension which would autoload fixtures for you.
After installing it you would have class yii\codeception\DbTestCase available, PersonTest should extend it.
Person fixture should have namespace as follows: app\tests\fixtures.

Laravel 5.1 foreign keys in model factory

How do you define foreign keys in a model factory. For example if I have a organisations table which has a foreign key to the countries table, in my model factory I'm having to define a dummy value for the country id as follows:
$factory->define(App\Organisation::class, function ($faker) {
return [
'name' => $faker->company,
'country_id' => 197,
];
});
In my organisations table seeder class I am doing the following but the faker object isn't present - do I need to create a new faker object in my seeder class?
use Illuminate\Database\Seeder;
class OrganisationsTableSeeder extends Seeder
{
public function run()
{
$countryIds = Country::lists('id')->all();
factory('App\Organisation', 3)->create([
// override factory default
'country_id' => $faker->randomElement[$countryIds],
]);
}
}
Database seeder class
class DatabaseSeeder extends Seeder
{
public function run()
{
Model::unguard();
$this->call('CountriesTableSeeder');
$this->call('OrganisationsTableSeeder');
Model::reguard();
}
}
Whats the best way to define the foreign keys when defining model factories? Is it possible to omit the country_id from the model factory and add it in the seeder class instead - from the documention it seems you can only override an existing value defined in the model factory but you cant add a new value via the seeder class - correct me if i'm wrong?
I may be a bit late on this one but I was having the same issue, this fixed it for me. You should be able to do
$factory->define(App\Organisation::class, function ($faker) {
return [
'name' => $faker->company,
'country_id' => factory(App\Country::class)->create()->id,
];
});
and then in your seed you just need to call
factory(App\Organisation::class, 5)->create();
and it will create the countries for you as well.
The way that the Laravel team, Otwell, Stauffer et.al., suggest is like this.
Testing > Adding Relations To Models
Adding relationships to your models
ModelFactory.php
$factory->define(App\Organisation::class, function ($faker) {
return [
'name' => $faker->company,
'country_id' => 197,
];
});
$factory->define(App\Country::class, function ($faker) {
return [
'name' => $faker->country,
];
});
seeder
$organisations = factory('App\Organisation', 3)
->create()
->each(function($$organisation) {
$organisation->relatedItems()->save(factory('App\Country')->make());
});

Accessing Model in CakePHP Controller Test

I'm new to CakePHP, and I just started writing my first tests. Usually doing Ruby on Rails, my approach to testing a Controller::create action would be to call the create action, and then comparing the number of models before and after that call, making sure it increased by one.
Would anyone test this any other way?
Is there an easy (builtin) way to access models from a ControllerTest in CakePHP? I couldn't find anything in the source, and accessing it through the Controller seems wrong.
I ended up doing something like this:
class AbstractControllerTestCase extends ControllerTestCase {
/**
* Load models, to be used like $this->DummyModel->[...]
* #param array
*/
public function loadModels() {
$models = func_get_args();
foreach ($models as $modelClass) {
$name = $modelClass . 'Model';
if(!isset($this->{$name})) {
$this->{$name} = ClassRegistry::init(array(
'class' => $modelClass, 'alias' => $modelClass
));
}
}
}
}
Then my tests inherit from AbstractControllerTestCase, call $this->loadModels('User'); in setUp and can do something like this in the test:
$countBefore = $this->UserModel->find('count');
// call the action with POST params
$countAfter = $this->UserModel->find('count');
$this->assertEquals($countAfter, $countBefore + 1);
Note that I'm new to CakePHP but came here with this question. Here's what I ended up doing.
I got my idea from #amiuhle, but I just do it manually in setUp, like how they mention in the model tests at http://book.cakephp.org/2.0/en/development/testing.html.
public function setUp() {
$this->Signup = ClassRegistry::init('Signup');
}
public function testMyTestXYZ() {
$data = array('first_name' => 'name');
$countBefore = $this->Signup->find('count');
$result = $this->testAction('/signups/add',
array(
'data' => array(
'Signup' => $data)
)
);
$countAfter = $this->Signup->find('count');
$this->assertEquals($countAfter, $countBefore + 1);
}
I am not sure why it is necessary to test how many times a model is called or instantiated from the controller action.
So, if I was testing Controller::create... my ControllerTest would contain something like:
testCreate(){
$result = $this->testAction('/controller/create');
if(!strpos($result,'form')){
$this->assertFalse(true);
}
$data = array(
'Article' => array(
'user_id' => 1,
'published' => 1,
'slug' => 'new-article',
'title' => 'New Article',
'body' => 'New Body'
)
);
$result = $this->testAction(
'/controller/create',
array('data' => $data, 'method' => 'post')
);
if(!strpos($result,'Record has been successfully created')){
$this->assertFalse(true);
}
}
The main things you want to test for is whether you are getting the right output for the input. And you can use xDebug profiler to easily find out what classes get instnantiated in a particular action and even how many times. There is no need to test for that manually!