Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mikes CS 128 #325

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Drupal\taxonomy\TermInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Rest endpoint to provide data about what entities are tagged with terms.
Expand Down Expand Up @@ -42,6 +43,13 @@ class TermsUsedResource extends ResourceBase {
*/
protected $fieldManager;

/**
* Current request.
*
* @var \Symfony\Component\HttpFoundation\Request|null
*/
protected $request;

/**
* {@inheritDoc}
*/
Expand All @@ -53,17 +61,19 @@ public static function create(ContainerInterface $container, array $configuratio
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('rest'),
$container->get('entity_type.manager'),
$container->get('entity_field.manager')
$container->get('entity_field.manager'),
$container->get('request_stack')
);
}

/**
* {@inheritDoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $field_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $field_manager, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->entityTypeManager = $entityTypeManager;
$this->fieldManager = $field_manager;
$this->request = $request_stack->getCurrentRequest();
}

/**
Expand Down Expand Up @@ -101,6 +111,32 @@ public function get($node_type, $include_children = TRUE) {
}
}

$params = $this->request->query->all();
$filtering_params = array_intersect(array_keys($params), array_keys($data));
$filtered_ids = [];

if ($filtering_params) {
foreach ($filtering_params as $field) {
foreach ($params[$field] as $value) {
$key = array_search($value, array_column($data[$field], 'id'));
$filtered_ids = $filtered_ids ? array_intersect($filtered_ids, $data[$field][$key]['items']) : $data[$field][$key]['items'];
}
}
}

if ($filtered_ids) {
foreach ($data as &$field_values) {
foreach ($field_values as $key => &$field_value) {
$field_value['items'] = array_values(array_intersect($filtered_ids, $field_value['items']));
if (!$field_value['items']) {
unset($field_values[$key]);
}
}

$field_values = array_values($field_values);
}
}

$data = array_filter($data);

$response = new ResourceResponse();
Expand Down Expand Up @@ -163,7 +199,7 @@ protected function getFieldTermsData(FieldConfigInterface $field, $include_child
}
}

uasort($data, function ($item_a, $item_b) {
uasort($data, function($item_a, $item_b) {
return count($item_a['items']) > count($item_b['items']) ? -1 : 1;
});
return array_values($data);
Expand Down
4 changes: 2 additions & 2 deletions themes/cardinal_service/pdb_components/Components/Filters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ export class Filters extends Component {
*/
componentDidMount() {
const that = this;
const params = this.props.apiParams ? "?" + queryString.stringify(this.props.apiParams, {arrayFormat: 'bracket'}) : ""

fetch('/api/terms-used/' + this.props.bundle)
fetch('/api/terms-used/' + this.props.bundle + params)
.then((response) => response.json())
.then((jsonData) => {
that.setState(
Expand All @@ -141,7 +142,6 @@ export class Filters extends Component {
const query = encodeURI(queryString.stringify(this.state.filters, {arrayFormat: 'bracket'}));
const location = this.props.submitUrl ?? window.location.pathname;
window.location = `${location}?${query}#filter-wrapper`;

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ var Filters = /*#__PURE__*/function (_Component) {
var _this2 = this;

var that = this;
fetch('/api/terms-used/' + this.props.bundle).then(function (response) {
var params = this.props.apiParams ? "?" + queryString.stringify(this.props.apiParams, {
arrayFormat: 'bracket'
}) : "";
fetch('/api/terms-used/' + this.props.bundle + params).then(function (response) {
return response.json();
}).then(function (jsonData) {
that.setState({
Expand Down Expand Up @@ -811,6 +814,9 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var _Components_Filters__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../Components/Filters */ "./Components/Filters.jsx");
/* harmony import */ var _styles_scss__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./styles.scss */ "./cardinal_quarter_opportunities_list/src/styles.scss");
/* harmony import */ var _styles_scss__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_styles_scss__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var query_string__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! query-string */ "./node_modules/query-string/index.js");
/* harmony import */ var query_string__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(query_string__WEBPACK_IMPORTED_MODULE_4__);




Expand Down Expand Up @@ -863,6 +869,9 @@ react_dom__WEBPACK_IMPORTED_MODULE_1___default.a.render( /*#__PURE__*/react__WEB
header: /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("h2", null, "Search by"),
wrapperAttributes: {
className: "flex-12-of-12"
},
apiParams: {
su_opp_dimension: ["301"]
}
}, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div", {
className: "flex-12-of-12 sort-links"
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import {Filters} from '../../Components/Filters';
import './styles.scss';
import queryString from 'query-string';

const nodeBundle = 'su_opportunity';
const nodeFields = [
Expand Down Expand Up @@ -37,6 +38,7 @@ ReactDOM.render(
fields={nodeFields}
header={<h2>Search by</h2>}
wrapperAttributes={{className: "flex-12-of-12"}}
apiParams={{su_opp_dimension: ["301"]}}
>
<div className="flex-12-of-12 sort-links">
<span style={{ fontWeight: 'bold' }} aria-hidden={true}>Sort By:</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ var Filters = /*#__PURE__*/function (_Component) {
var _this2 = this;

var that = this;
fetch('/api/terms-used/' + this.props.bundle).then(function (response) {
var params = this.props.apiParams ? "?" + queryString.stringify(this.props.apiParams, {
arrayFormat: 'bracket'
}) : "";
fetch('/api/terms-used/' + this.props.bundle + params).then(function (response) {
return response.json();
}).then(function (jsonData) {
that.setState({
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ var Filters = /*#__PURE__*/function (_Component) {
var _this2 = this;

var that = this;
fetch('/api/terms-used/' + this.props.bundle).then(function (response) {
var params = this.props.apiParams ? "?" + queryString.stringify(this.props.apiParams, {
arrayFormat: 'bracket'
}) : "";
fetch('/api/terms-used/' + this.props.bundle + params).then(function (response) {
return response.json();
}).then(function (jsonData) {
that.setState({
Expand Down

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion themes/cardinal_service/pdb_components/resources/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ var Filters = /*#__PURE__*/function (_Component) {
var _this2 = this;

var that = this;
fetch('/api/terms-used/' + this.props.bundle).then(function (response) {
var params = this.props.apiParams ? "?" + queryString.stringify(this.props.apiParams, {
arrayFormat: 'bracket'
}) : "";
fetch('/api/terms-used/' + this.props.bundle + params).then(function (response) {
return response.json();
}).then(function (jsonData) {
that.setState({
Expand Down

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion themes/cardinal_service/pdb_components/stories/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ var Filters = /*#__PURE__*/function (_Component) {
var _this2 = this;

var that = this;
fetch('/api/terms-used/' + this.props.bundle).then(function (response) {
var params = this.props.apiParams ? "?" + queryString.stringify(this.props.apiParams, {
arrayFormat: 'bracket'
}) : "";
fetch('/api/terms-used/' + this.props.bundle + params).then(function (response) {
return response.json();
}).then(function (jsonData) {
that.setState({
Expand Down

Large diffs are not rendered by default.

Loading