Argument 1 passed to EntityViewBuilder must implement interface, null given - drupal-views

In my Drupal project I have this error:
Recoverable fatal error: Argument 1 passed to
Drupal\Core\Entity\EntityViewBuilder::view() must implement interface
Drupal\Core\Entity\EntityInterface, null given, called in
/Users/team1/workspace/ga_p/modules/field_collection/src/Plugin/Field/FieldFormatter/FieldCollectionItemsFormatter.php
on line 33 and defined in Drupal\Core\Entity\EntityViewBuilder->view()
(line 110 of core/lib/Drupal/Core/Entity/EntityViewBuilder.php). I do
not know if it's from having created multiple fields
(fieldCollections)
But I do not know how to remove this error.
Drupal Version: 8.3.4

Try this patch
diff --git a/src/Plugin/Field/FieldType/FieldCollection.php b/src/Plugin/Field/FieldType/FieldCollection.php
index 8721220..c5335be 100644
--- a/src/Plugin/Field/FieldType/FieldCollection.php
+++ b/src/Plugin/Field/FieldType/FieldCollection.php
## -183,19 +183,15 ## class FieldCollection extends EntityReferenceItem {
public function preSave() {
if ($field_collection_item = $this->getFieldCollectionItem()) {
- // TODO: Handle node cloning
- /*
- if (!empty($host_entity->is_new) && empty($entity->is_new)) {
+ $host = $this->getEntity();
+
+ // Handle node cloning
+ if($host->isNew() && !$field_collection_item->isNew()) {
// If the host entity is new but we have a field_collection that is not
// new, it means that its host is being cloned. Thus we need to clone
// the field collection entity as well.
- $new_entity = clone $entity;
- $new_entity->target_id = NULL;
- $new_entity->revision_id = NULL;
- $new_entity->is_new = TRUE;
- $entity = $new_entity;
+ $field_collection_item = $field_collection_item->createDuplicate();
}
- */
// TODO: Handle deleted items
/*
## -231,11 +227,10 ## class FieldCollection extends EntityReferenceItem {
}
*/
- $this->newHostRevision = $this->getEntity()->isNewRevision();
+ $this->newHostRevision = $host->isNewRevision();
// If the host entity is saved as new revision, do the same for the item.
if ($this->newHostRevision) {
- $host = $this->getEntity();
$field_collection_item->setNewRevision();
diff --git a/src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php b/src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php
index 4d6ae09..800383f 100644
--- a/src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php
+++ b/src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php
## -104,9 +104,8 ## class FieldCollectionEmbedWidget extends WidgetBase {
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
// We don't want to render empty items on field collection fields
// unless a) the field collection is empty ; b) the form is rebuilding,
- // which means that the user clicked on "Add another item"; or
- // c) we are creating a new entity.
- if ((count($items) > 0) && !$form_state->isRebuilding() && !$items->getEntity()->isNew()) {
+ // which means that the user clicked on "Add another item"
+ if ((count($items) > 0) && !$form_state->isRebuilding()) {
$field_name = $this->fieldDefinition->getName();
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
$parents = $form['#parents'];
mentioned here.

Related

How to append CSV file with PostgreSQL query export in Datagrip?

I want to append an existing CSV file (Template for Google Ads Offline Conversion Tracking) with the results of a query from PostgreSQL using Datagrip.
Execute to file works just fine to export the query results as a CSV file. But obviously that just creates a new file with only the results of the query.
I was thinking to load the existing template in the groovy script that's there in datagrip, and then add its content to the top of the created file, but I can't make any headway on how to do this.
I found (maybe?) how to load the file, but I have no clue how to actually use this and put it at the top of the file so the results of the query can be added below it.
fh = new File("C:/1.csv")
def csv_content = fh.getText('utf-8')
That's all I got for loading the file with no clue on how to use this going forward.
Alternatively I considered to just manually add the info of the template by hand to the script, so it doesn't have to load another file, but just takes that info and then adds the query results afterwards. If you could give me a way on how to directly add rows like these would be very helpful too.
The groovy script file that is included in Datagrip is as follows:
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
SEPARATOR = ","
QUOTE = "\""
NEWLINE = System.getProperty("line.separator")
def printRow = { values, valueToString ->
values.eachWithIndex { value, idx ->
def str = valueToString(value)
def q = str.contains(SEPARATOR) || str.contains(QUOTE) || str.contains(NEWLINE)
OUT.append(q ? QUOTE : "")
.append(str.replace(QUOTE, QUOTE + QUOTE))
.append(q ? QUOTE : "")
.append(idx != values.size() - 1 ? SEPARATOR : NEWLINE)
}
}
if (!TRANSPOSED) {
ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) }
}
else {
def values = COLUMNS.collect { new ArrayList<String>() }
ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } }
values.each { printRow(it, { it }) }
}
You need to pass content of your file to OUT variable like this:
fh = new File("C:/1.csv")
def csv_content = fh.getText('utf-8')
OUT.append(csv_content).append("\n")
Everything that is passed to OUT.append will be written to new file.
Just add it to the beginning of the script:
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
SEPARATOR = ","
QUOTE = "\""
NEWLINE = System.getProperty("line.separator")
fh = new File("C:/1.csv")
def csv_content = fh.getText('utf-8')
OUT.append(csv_content).append("\n")
def printRow = { values, valueToString ->
values.eachWithIndex { value, idx ->
def str = valueToString(value)
def q = str.contains(SEPARATOR) || str.contains(QUOTE) || str.contains(NEWLINE)
OUT.append(q ? QUOTE : "")
.append(str.replace(QUOTE, QUOTE + QUOTE))
.append(q ? QUOTE : "")
.append(idx != values.size() - 1 ? SEPARATOR : NEWLINE)
}
}
if (!TRANSPOSED) {
ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) }
}
else {
def values = COLUMNS.collect { new ArrayList<String>() }
ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } }
values.each { printRow(it, { it }) }
}
Note that you may copy paste CSV-Groovy.csv.groovy to the same directory (e.g. MY-CSV.csv.groovy) and modify new file. This new extractor will be added to combobox with all extractors

FileSystemProvider auto-create directory

I'm working on implementing this the File System provider functionality:
https://code.visualstudio.com/api/references/vscode-api#FileSystemProvider
I'd like the following writeFile function to automatically create any necessary parent directories as needed. Was wondering if any one else has done anything similar, or how the experts would approach it.
https://github.com/microsoft/vscode-extension-samples/blob/aa42c2ec7248059ff5903ff11aa6eeabc04329bf/fsprovider-sample/src/fileSystemProvider.ts#L80
Option 1:
change the following line to silent=true and if it returns an undefined, create the necessary directory
https://github.com/microsoft/vscode-extension-samples/blob/aa42c2ec7248059ff5903ff11aa6eeabc04329bf/fsprovider-sample/src/fileSystemProvider.ts#L199
option 2:
Extend the logic of this if statement or replace the following error with some logic to create the folders
https://github.com/microsoft/vscode-extension-samples/blob/aa42c2ec7248059ff5903ff11aa6eeabc04329bf/fsprovider-sample/src/fileSystemProvider.ts#L183
thoughts or suggestions?
I was able to get this working with the following function. I tried the follow the existing code style and approach for simplicity.
/**
* make parent directires if not present
*/
private _makePath(uri: Uri) {
// get the base directory
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
// split the base directory to the folder parts
const parts = dirname.path.split('/');
// array to hold the parent folders as we create them
const fullPath: string[] = [];
// loop through folders
for ( const part of parts) {
if (!part) {
continue; // empty part, continue with loop
}
// track the folder path as we check/create them
fullPath.push(part);
// see if current folder exists
const here = this._lookup(Uri.parse(...fullPath), true);
if (!here) {
// current folder not found, so create it
this.createDirectory(Uri.parse(path.join(...fullPath)));
} else {
console.log('directory exists already: ', part);
}
}
}
Then call the function at each file creation:
writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean, overwrite: boolean }, id?: string): void {
console.log('writefile: ', uri);
const basename = path.posix.basename(uri.path);
// *** create dir path if not there ***
this._makePath(uri);
const parent = this._lookupParentDirectory(uri);
let entry = parent.entries.get(basename);
if (entry instanceof Directory) {
throw vscode.FileSystemError.FileIsADirectory(uri);
}
if (!entry && !options.create) {
throw vscode.FileSystemError.FileNotFound(uri);
}
if (entry && options.create && !options.overwrite) {
throw vscode.FileSystemError.FileExists(uri);
}
if (!entry) {
entry = new File(basename, id);
parent.entries.set(basename, entry);
this._fireSoon({ type: vscode.FileChangeType.Created, uri });
}
entry.mtime = Date.now();
entry.size = content.byteLength;
entry.data = content;
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
}

IQueryable in foreach - open DataReader error

When i run this code
public PartialViewResult GetHardware()
{
IQueryable<Hardware> hardware = db.Hardwares;
HardwareState hwState = new HardwareState();
IQueryable<IGrouping<string, Hardware>> groupByCategory = hardware.GroupBy(g => g.Category);
foreach (IGrouping<string, Hardware> group in groupByCategory)
{
hwState.GroupName = group.Key;
hwState.GroupUnitsCount = group.Count();
hwState.StorageReservedCount
= group.Where(m =>
m.Place.IsStorage == true &&
m.PlaceID != (int)Constants.HardwareState.Created &&
m.HardwareState == (int)Constants.HardwareState.Reserved).Count();
}
return PartialView(hwState);
}
I get an error about that the navigation property m.Place = null
when i transfer some of the text with the code of the foreach block
public PartialViewResult GetHardware()
{
IQueryable<Hardware> hardware = db.Hardwares;
HardwareState hwState = new HardwareState();
IQueryable<IGrouping<string, Hardware>> groupByCategory = hardware.GroupBy(g => g.Category);
hwState.StorageReservedCount
= hardware.Where(m =>
m.Place.IsStorage == true &&
m.PlaceID != (int)Constants.HardwareState.Created &&
m.HardwareState == (int)Constants.HardwareState.Reserved).Count();
foreach (IGrouping<string, Hardware> group in groupByCategory)
{
hwState.GroupName = group.Key;
hwState.GroupUnitsCount = group.Count();
}
return PartialView(hwState);
}
,the navigation property is not set to null and the error does not appear
Extension methods such as .AsQueryable or Include(x => x.Place) do not help me
How can i solve this problem?
UPDATE: If i change the type to IEnumerable instead IQueryable it begins to work!
but i would like to work with the IQueryable type
UPDATE2: I'm sorry, i did not put it correctly when i wrote that the error is an empty navigation property. Error that appears in fact
"There is already an open DataReader associated with this Command which must be closed first."
As described in this answer it is because: (quote)
"Another scenario when this always happens is when you iterate through
result of the query (IQueryable) and you will trigger lazy loading for
loaded entity inside the iteration."
But it does not say how to solve the problem without using ToList () or MARS

Trying to work with down() method from ExtJS 4.2.1

I am trying to find a specific element from my page using ExtJS 4 so I can do modifications on it.
I know its id so it should not be a problem BUT
-I tried Ext.getCmp('theId') and it just return me undefined
-I tried to use down('theId') method by passing through the view and I still get a nullresponse.
As I know the id of the element I tried again the two methods by setting manually the id and it didn't work neither.
Do these two methods not function?
How should I do?
Here is the concerned part of the code :
listeners: {
load: function(node, records, successful, eOpts) {
var ownertree = records.store.ownerTree;
var boundView = ownertree.dockedItems.items[1].view.id;
var generalId = boundView+'-record-';
// Add row stripping on leaf nodes when a node is expanded
//
// Adding the same feature to the whole tree instead of leaf nodes
// would not be much more complicated but it would require iterating
// the whole tree starting with the root node to build a list of
// all visible nodes. The same function would need to be called
// on expand, collapse, append, insert, remove and load events.
if (!node.tree.root.data.leaf) {
// Process each child node
node.tree.root.cascadeBy(function(currentChild) {
// Process only leaf
if (currentChild.data.leaf) {
var nodeId = ""+generalId+currentChild.internalId;
var index = currentChild.data.index;
if ((index % 2) == 0) {
// even node
currentChild.data.cls.replace('tree-odd-node', '')
currentChild.data.cls = 'tree-even-node';
} else {
// odd node
currentChild.data.cls.replace('tree-even-node', '')
currentChild.data.cls = 'tree-odd-node';
}
// Update CSS classes
currentChild.triggerUIUpdate();
console.log(nodeId);
console.log(ownertree.view.body);
console.log(Ext.getCmp(nodeId));
console.log(Ext.getCmp('treeview-1016-record-02001001'));
console.log(ownertree.view.body.down(nodeId));
console.log(ownertree.view.body.down('treeview-1016-record-02001001'));
}
});
}
}
You can see my console.log at the end.
Here is what they give me on the javascript console (in the right order):
treeview-1016-record-02001001
The precise id I am looking for. And I also try manually in case...
h {dom: table#treeview-1016-table.x-treeview-1016-table x-grid-table, el: h, id: "treeview-1016gridBody", $cache: Object, lastBox: Object…}
I checked every configs of this item and its dom and it is exactly the part of the dom I am looking for, which is the view containing my tree. The BIG parent
And then:
undefined
undefined
null
null
Here is the item I want to access:
<tr role="row" id="treeview-1016-record-02001001" ...>
And I checked there is no id duplication anywhere...
I asked someone else who told me these methods do not work. The problem is I need to access this item to modify its cls.
I would appreciate any idea.
You are looking for Ext.get(id). Ext.getCmp(id) is used for Ext.Components, and Ext.get(id) is used for Ext.dom.Elements. See the docs here: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext-method-get
Ok so finally I used the afteritemexpand listener. With the ids I get the elements I am looking for with your Ext.get(id) method kevhender :).
The reason is that the dom elements where not completely loaded when I used my load listener (it was just the store) so the Ext.get(id) method couldn't get the the element correctly. I first used afterlayout listener, that was correct but too often called and the access to the id was not so easy.
So, here is how I did finally :
listeners: {
load: function(node, records, successful, eOpts) {
var ownertree = records.store.ownerTree;
var boundView = ownertree.dockedItems.items[1].view.id;
var generalId = boundView+'-record-';
if (!node.tree.root.data.leaf) {
// Process each child node
node.tree.root.cascadeBy(function(currentChild) {
// Process only leaf
if (currentChild.data.leaf) {
var nodeId = ""+generalId+currentChild.internalId;
var index = currentChild.data.index;
if ( (index % 2) == 0 && ids.indexOf(nodeId) == -1 ) {
ids[indiceIds] = nodeId;
indiceIds++;
}
console.log(ids);
}
});
}
},
afteritemexpand: function( node, index, item, eOpts ){
/* This commented section below could replace the load but the load is done during store loading while afteritemexpand is done after expanding an item.
So, load listener makes saving time AND makes loading time constant. That is not the case if we just consider the commented section below because
the more you expand nodes, the more items it will have to get and so loading time is more and more important
*/
// var domLeaf = Ext.get(item.id).next();
// for ( var int = 0; int < node.childNodes.length; int++) {
// if (node.childNodes[int].data.leaf && (int % 2) == 0) {
// if (ids.indexOf(domLeaf.id) == -1) {
// ids[indiceIds] = domLeaf.id;
// indiceIds++;
// }
// }
// domLeaf = domLeaf.next();
// }
for ( var int = 0; int < ids.length; int++) {
domLeaf = Ext.get(ids[int]);
if (domLeaf != null) {
for ( var int2 = 0; int2 < domLeaf.dom.children.length; int2++) {
if (domLeaf.dom.children[int2].className.search('tree-even-node') == -1){
domLeaf.dom.children[int2].className += ' tree-even-node';
}
}
}
}
},
With ids an Array of the ids I need to set the class.
Thank you for the method.

Set Time Range of a Zend Dojo TextTimeBox

Hi is it possible to set the time range of a Dojo textTimeBox to 09:00 - 18:30.
I can't find anything in either the Zend or Dojo documentation that show how this can be done or if it can be done.
Many thanks in advance.
You can set max and min constraints for widget:
new dijit.form.TimeTextBox({
name: "prog_val",
value: new Date(),
constraints: {
timePattern: 'HH:mm:ss',
clickableIncrement: 'T00:15:00',
visibleIncrement: 'T00:15:00',
visibleRange: 'T01:00:00',
min:'T09:00:00',
max:'T18:30:00'
}
},
"prog_val");
It does not allow the user to enter data beyond the allowed values.
However this still allows user to scroll to the disabled times, user just cannot select them.
For hiding disabled times you should do some hack :)
You should override _getFilteredNodes method of dijit._TimePicker. For example :
dojo.declare("my._TimePicker", dijit._TimePicker, {
// extend the default show() method
_getFilteredNodes: function (/*number*/start, /*number*/maxNum, /*Boolean*/before) {
// summary:
// Returns an array of nodes with the filter applied. At most maxNum nodes
// will be returned - but fewer may be returned as well. If the
// before parameter is set to true, then it will return the elements
// before the given index
// tags:
// private
var nodes = [], n, i = start, max = this._maxIncrement + Math.abs(i),
chk = before ? -1 : 1, dec = before ? 1 : 0, inc = before ? 0 : 1;
do {
i = i - dec;
var date = new Date(this._refDate);
var incrementDate = this._clickableIncrementDate;
date.setHours(date.getHours() + incrementDate.getHours() * i,
date.getMinutes() + incrementDate.getMinutes() * i,
date.getSeconds() + incrementDate.getSeconds() * i);
if (!this.isDisabledDate(date)) {
n = this._createOption(i);
if (n) { nodes.push(n); }
}
i = i + inc;
} while (nodes.length < maxNum && (i * chk) < max);
if (before) { nodes.reverse(); }
return nodes;
}
});
And you need to set this new class ('my._TimePicker') as a popupClass property of your text time box:
dojo.addOnLoad(function () {
dijit.byId("prog_val").popupClass = "my._TimePicker";
});
And you can see : it works!